Exemplo n.º 1
0
 public SetCodec(Type type, CodecInfoWithAttributes elementCodecInfo)
 {
     this.type             = type;
     this.elementCodecInfo = elementCodecInfo;
     this.addMethod        = type.GetMethod("Add");
     this.countProperty    = type.GetProperty("Count");
 }
Exemplo n.º 2
0
        public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs)
        {
            Type type = codecInfoWithAttrs.Info.type;

            if (!type.IsGenericType || !ReferenceEquals(type.GetGenericTypeDefinition(), typeof(Dictionary <,>)))
            {
                return(null);
            }
            Type type2         = type.GetGenericArguments()[0];
            Type type3         = type.GetGenericArguments()[1];
            bool optional      = false;
            bool varied        = false;
            bool optionalValue = false;
            bool variedValue   = false;

            if (codecInfoWithAttrs.IsAttributePresent <ProtocolDictionaryAttribute>())
            {
                ProtocolDictionaryAttribute attribute = codecInfoWithAttrs.GetAttribute <ProtocolDictionaryAttribute>();
                optional      = attribute.OptionalKey;
                varied        = attribute.VariedKey;
                optionalValue = attribute.OptionalValue;
                variedValue   = attribute.VariedValue;
            }
            return(new DictionaryCodec(type, new CodecInfoWithAttributes(type2, optional, varied), new CodecInfoWithAttributes(type3, optionalValue, variedValue)));
        }
Exemplo n.º 3
0
        private Codec GetCodec(CodecInfo info)
        {
            Codec codec;
            Codec codec2;

            if (this.codecRegistry.TryGetValue(info, out codec))
            {
                return(codec);
            }
            using (Dictionary <Type, Codec> .KeyCollection.Enumerator enumerator = this.hierarchicalCodecRegistry.Keys.GetEnumerator())
            {
                while (true)
                {
                    if (enumerator.MoveNext())
                    {
                        Type current = enumerator.Current;
                        if (!current.IsAssignableFrom(info.type))
                        {
                            continue;
                        }
                        codec2 = this.hierarchicalCodecRegistry[current];
                    }
                    else
                    {
                        CodecInfoWithAttributes codecInfoWithAttributes = new CodecInfoWithAttributes(info);
                        return(this.CreateCodecIfNecessary(codecInfoWithAttributes));
                    }
                    break;
                }
            }
            return(codec2);
        }
Exemplo n.º 4
0
        public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs)
        {
            Type structType = !ReflectionUtils.IsNullableType(codecInfoWithAttrs.Info.type) ? codecInfoWithAttrs.Info.type : ReflectionUtils.GetNullableInnerType(codecInfoWithAttrs.Info.type);
            List <PropertyInfo>    sortedProperties = GetSortedProperties(structType, protocol);
            List <PropertyRequest> requests         = new List <PropertyRequest>(sortedProperties.Count);

            foreach (PropertyInfo info4 in sortedProperties)
            {
                bool optional = Attribute.IsDefined(info4, typeof(ProtocolOptionalAttribute));
                bool varied   = Attribute.IsDefined(info4, typeof(ProtocolVariedAttribute));
                CodecInfoWithAttributes codecInfoWithAttributes = new CodecInfoWithAttributes(info4.PropertyType, optional, varied);
                object[] objArray2 = info4.GetCustomAttributes(true);
                int      index     = 0;
                while (true)
                {
                    if (index >= objArray2.Length)
                    {
                        requests.Add(new PropertyRequest(info4, codecInfoWithAttributes));
                        break;
                    }
                    Attribute attribute = (Attribute)objArray2[index];
                    codecInfoWithAttributes.AddAttribute(attribute);
                    index++;
                }
            }
            return(new StructCodec(structType, requests));
        }
Exemplo n.º 5
0
 public OptionalTypeCodec(Type type, CodecInfoWithAttributes elementCodecInfo)
 {
     this.type             = type;
     this.elementCodecInfo = elementCodecInfo;
     this.emptyMethod      = type.GetMethod("empty");
     this.ofMethod         = type.GetMethod("of");
     this.isPresentMethod  = type.GetMethod("IsPresent");
     this.getMethod        = type.GetMethod("Get");
 }
Exemplo n.º 6
0
        public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs)
        {
            EnumCodec codec;
            Type      key = codecInfoWithAttrs.Info.type;

            if (!key.IsEnum)
            {
                return(null);
            }
            if (!this.codecs.TryGetValue(key, out codec))
            {
                codec            = new EnumCodec(key);
                this.codecs[key] = codec;
            }
            return(codec);
        }
Exemplo n.º 7
0
        public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs)
        {
            Type type = codecInfoWithAttrs.Info.type;

            if (!type.IsGenericType || !ReferenceEquals(type.GetGenericTypeDefinition(), typeof(List <>)))
            {
                return(null);
            }
            Type type2    = type.GetGenericArguments()[0];
            bool optional = false;
            bool varied   = false;

            if (codecInfoWithAttrs.IsAttributePresent <ProtocolCollectionAttribute>())
            {
                ProtocolCollectionAttribute attribute = codecInfoWithAttrs.GetAttribute <ProtocolCollectionAttribute>();
                optional = attribute.Optional;
                varied   = attribute.Varied;
            }
            return(new ListCodec(type, new CodecInfoWithAttributes(type2, optional, varied)));
        }
Exemplo n.º 8
0
        public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs)
        {
            Type type = codecInfoWithAttrs.Info.type;

            if (!type.IsArray)
            {
                return(null);
            }
            Type elementType = type.GetElementType();
            bool optional    = false;
            bool varied      = false;

            if (codecInfoWithAttrs.IsAttributePresent <ProtocolCollectionAttribute>())
            {
                ProtocolCollectionAttribute attribute = codecInfoWithAttrs.GetAttribute <ProtocolCollectionAttribute>();
                optional = attribute.Optional;
                varied   = attribute.Varied;
            }
            return(new ArrayCodec(elementType, new CodecInfoWithAttributes(type.GetElementType(), optional, varied)));
        }
Exemplo n.º 9
0
        private Codec CreateCodecIfNecessary(CodecInfoWithAttributes codecInfoWithAttributes)
        {
            Codec     codec;
            CodecInfo info = codecInfoWithAttributes.Info;

            if (ReflectionUtils.IsNullableType(info.type))
            {
                CodecInfo key = new CodecInfo(ReflectionUtils.GetNullableInnerType(info.type), info.optional, info.varied);
                if (this.codecRegistry.TryGetValue(key, out codec))
                {
                    return(codec);
                }
            }
            for (int i = 0; i < this.factories.Count; i++)
            {
                codec = this.factories[i].CreateCodec(this, codecInfoWithAttributes);
                if (codec != null)
                {
                    if (info.optional)
                    {
                        codec = new OptionalCodec(codec);
                    }
                    this.RegisterCodec(info, codec);
                    return(codec);
                }
            }
            for (Type type2 = info.type.BaseType; type2 != null; type2 = type2.BaseType)
            {
                CodecInfo key = new CodecInfo(type2, info.optional, info.varied);
                if (this.codecRegistry.TryGetValue(key, out codec))
                {
                    this.codecRegistry.Add(info, codec);
                    return(codec);
                }
            }
            throw new CodecNotFoundForRequestException(info);
        }
Exemplo n.º 10
0
 public ListCodec(Type type, CodecInfoWithAttributes elementCodecInfo)
 {
     this.type             = type;
     this.elementCodecInfo = elementCodecInfo;
 }
Exemplo n.º 11
0
 public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs) =>
 !codecInfoWithAttrs.Info.varied ? null : (!ReferenceEquals(codecInfoWithAttrs.Info.type, typeof(Type)) ? ((Codec)this.structCodec) : ((Codec)this.typeCodec));
Exemplo n.º 12
0
 public DictionaryCodec(Type type, CodecInfoWithAttributes keyRequest, CodecInfoWithAttributes valueRequest)
 {
     this.type         = type;
     this.keyRequest   = keyRequest;
     this.valueRequest = valueRequest;
 }
Exemplo n.º 13
0
        public Codec CreateCodec(Protocol protocol, CodecInfoWithAttributes codecInfoWithAttrs)
        {
            Type type = codecInfoWithAttrs.Info.type;

            return((!type.IsGenericType || !ReferenceEquals(type.GetGenericTypeDefinition(), typeof(Optional <>))) ? null : new OptionalTypeCodec(type, new CodecInfoWithAttributes(type.GetGenericArguments()[0], false, codecInfoWithAttrs.Info.varied)));
        }
Exemplo n.º 14
0
 public ArrayCodec(Type elementType, CodecInfoWithAttributes elementCodecInfo)
 {
     this.elementType      = elementType;
     this.elementCodecInfo = elementCodecInfo;
 }