/// <summary>
        /// Deserializes a byte array into an object of type <paramref name="type"/>.
        /// </summary>
        /// <param name="bytes">The array containing the serialized object</param>
        /// <param name="type">The type of object contained in the array</param>
        /// <returns>The object contained in the array</returns>
        /// <exception cref="NotSupportedException">Unknown SelectionEnvelope.Elements.Type</exception>
        public override object FromBinary(byte[] bytes, Type type)
        {
            SelectionEnvelope selectionEnvelope = SelectionEnvelope.ParseFrom(bytes);
            Type msgType = null;

            if (selectionEnvelope.HasMessageManifest)
            {
                msgType = Type.GetType(selectionEnvelope.MessageManifest.ToStringUtf8());
            }
            int    serializerId = selectionEnvelope.SerializerId;
            object msg          = Deserialize(selectionEnvelope.EnclosedMessage, msgType, serializerId);

            SelectionPathElement[] elements = selectionEnvelope.PatternList.Select <Selection, SelectionPathElement>(p =>
            {
                if (p.Type == PatternType.PARENT)
                {
                    return(new SelectParent());
                }
                if (p.Type == PatternType.CHILD_NAME)
                {
                    return(new SelectChildName(p.Matcher));
                }
                if (p.Type == PatternType.CHILD_PATTERN)
                {
                    return(new SelectChildPattern(p.Matcher));
                }
                throw new NotSupportedException("Unknown SelectionEnvelope.Elements.Type");
            }).ToArray();
            return(new ActorSelectionMessage(msg, elements));
        }
        /// <summary>
        /// Deserializes a byte array into an object of type <paramref name="type"/>.
        /// </summary>
        /// <param name="bytes">The array containing the serialized object</param>
        /// <param name="type">The type of object contained in the array</param>
        /// <returns>The object contained in the array</returns>
        /// <exception cref="NotSupportedException">
        /// This exception is thrown if the <see cref="SelectionEnvelope"/>, contained within the specified byte array
        /// <paramref name="bytes"/>, contains an unknown <see cref="PatternType"/>.
        /// </exception>
        public override object FromBinary(byte[] bytes, Type type)
        {
            SelectionEnvelope selectionEnvelope = SelectionEnvelope.ParseFrom(bytes);
            var manifest = selectionEnvelope.HasMessageManifest ? selectionEnvelope.MessageManifest.ToStringUtf8() : string.Empty;
            var message  = system.Serialization.Deserialize(
                selectionEnvelope.EnclosedMessage.ToByteArray(),
                selectionEnvelope.SerializerId,
                manifest);

            SelectionPathElement[] elements = selectionEnvelope.PatternList.Select <Selection, SelectionPathElement>(p =>
            {
                if (p.Type == PatternType.PARENT)
                {
                    return(new SelectParent());
                }
                if (p.Type == PatternType.CHILD_NAME)
                {
                    return(new SelectChildName(p.Matcher));
                }
                if (p.Type == PatternType.CHILD_PATTERN)
                {
                    return(new SelectChildPattern(p.Matcher));
                }
                throw new NotSupportedException("Unknown SelectionEnvelope.Elements.Type");
            }).ToArray();

            return(new ActorSelectionMessage(message, elements));
        }
        private byte[] SerializeActorSelectionMessage(ActorSelectionMessage sel)
        {
            SelectionEnvelope.Builder builder = SelectionEnvelope.CreateBuilder();
            Serializer serializer             = system.Serialization.FindSerializerFor(sel.Message);

            builder.SetEnclosedMessage(ByteString.CopyFrom(serializer.ToBinary(sel.Message)));
            builder.SetSerializerId(serializer.Identifier);
            if (serializer.IncludeManifest)
            {
                builder.SetMessageManifest(ByteString.CopyFromUtf8(sel.Message.GetType().AssemblyQualifiedName));
            }
            foreach (SelectionPathElement element in sel.Elements)
            {
                element.Match()
                .With <SelectChildName>(m => builder.AddPattern(BuildPattern(m.Name, PatternType.CHILD_NAME)))
                .With <SelectChildPattern>(
                    m => builder.AddPattern(BuildPattern(m.PatternStr, PatternType.CHILD_PATTERN)))
                .With <SelectParent>(m => builder.AddPattern(BuildPattern(null, PatternType.PARENT)));
            }

            return(builder.Build().ToByteArray());
        }