Beispiel #1
0
 public MapType(AmqpContractSerializer serializer, Type type, MemberAccessor keyAccessor, MemberAccessor valueAccessor, MethodAccessor addAccessor) : base(serializer, type)
 {
     this.keyType           = this.serializer.GetType(keyAccessor.Type);
     this.valueType         = this.serializer.GetType(valueAccessor.Type);
     this.keyAccessor       = keyAccessor;
     this.valueAccessor     = valueAccessor;
     this.addMethodAccessor = addAccessor;
 }
Beispiel #2
0
 protected DescribedType(AmqpContractSerializer serializer, Type type, SerializableType baseType, string descriptorName, ulong?descriptorCode, SerialiableMember[] members, Dictionary <Type, SerializableType> knownTypes, MethodAccessor onDesrialized) : base(serializer, type)
 {
     this.baseType       = (SerializableType.DescribedType)baseType;
     this.descriptorName = descriptorName;
     this.descriptorCode = descriptorCode;
     this.members        = members;
     this.onDeserialized = onDesrialized;
     this.knownTypes     = SerializableType.DescribedType.GetKnownTypes(knownTypes);
 }
Beispiel #3
0
 public override void ReadMembers(ByteBuffer buffer, object container, ref int count)
 {
     while (count > 0)
     {
         object         obj            = this.itemType.ReadObject(buffer);
         MethodAccessor methodAccessor = this.addMethodAccessor;
         object[]       objArray       = new object[] { obj };
         methodAccessor.Invoke(container, objArray);
         count = count - 1;
     }
 }
Beispiel #4
0
 protected void InvokeDeserialized(object container)
 {
     if (this.baseType != null)
     {
         this.baseType.InvokeDeserialized(container);
     }
     if (this.onDeserialized != null)
     {
         MethodAccessor methodAccessor = this.onDeserialized;
         object[]       objArray       = new object[] { new StreamingContext() };
         methodAccessor.Invoke(container, objArray);
     }
 }
        private SerializableType CompileInterfaceTypes(Type type)
        {
            bool           isArray         = type.IsArray;
            bool           flag            = false;
            bool           flag1           = false;
            MemberAccessor memberAccessor  = null;
            MemberAccessor memberAccessor1 = null;
            MethodAccessor methodAccessor  = null;
            Type           type1           = null;

            if (type.GetInterface(typeof(IAmqpSerializable).Name, false) != null)
            {
                return(SerializableType.CreateAmqpSerializableType(this, type));
            }
            Type[] interfaces = type.GetInterfaces();
            for (int i = 0; i < (int)interfaces.Length; i++)
            {
                Type type2 = interfaces[i];
                if (type2.IsGenericType)
                {
                    Type genericTypeDefinition = type2.GetGenericTypeDefinition();
                    if (genericTypeDefinition == typeof(IDictionary <,>))
                    {
                        flag = true;
                        Type[] genericArguments = type2.GetGenericArguments();
                        type1           = typeof(KeyValuePair <,>).MakeGenericType(genericArguments);
                        memberAccessor  = MemberAccessor.Create(type1.GetProperty("Key"), false);
                        memberAccessor1 = MemberAccessor.Create(type1.GetProperty("Value"), false);
                        methodAccessor  = MethodAccessor.Create(type.GetMethod("Add", genericArguments));
                        break;
                    }
                    else if (genericTypeDefinition == typeof(IList <>))
                    {
                        flag1 = true;
                        Type[] typeArray = type2.GetGenericArguments();
                        type1          = typeArray[0];
                        methodAccessor = MethodAccessor.Create(type.GetMethod("Add", typeArray));
                        break;
                    }
                }
            }
            if (flag)
            {
                return(SerializableType.CreateMapType(this, type, memberAccessor, memberAccessor1, methodAccessor));
            }
            if (isArray || !flag1)
            {
                return(null);
            }
            return(SerializableType.CreateListType(this, type, type1, methodAccessor));
        }
Beispiel #6
0
 public static SerializableType CreateMapType(AmqpContractSerializer serializer, Type type, MemberAccessor keyAccessor, MemberAccessor valueAccessor, MethodAccessor addAccessor)
 {
     return(new SerializableType.MapType(serializer, type, keyAccessor, valueAccessor, addAccessor));
 }
Beispiel #7
0
 public static SerializableType CreateListType(AmqpContractSerializer serializer, Type type, Type itemType, MethodAccessor addAccessor)
 {
     return(new SerializableType.ListType(serializer, type, itemType, addAccessor));
 }
Beispiel #8
0
 public ListType(AmqpContractSerializer serializer, Type type, Type itemType, MethodAccessor addAccessor) : base(serializer, type)
 {
     this.itemType          = serializer.GetType(itemType);
     this.addMethodAccessor = addAccessor;
 }
Beispiel #9
0
 public static SerializableType CreateDescribedMapType(AmqpContractSerializer serializer, Type type, SerializableType baseType, string descriptorName, ulong?descriptorCode, SerialiableMember[] members, Dictionary <Type, SerializableType> knownTypes, MethodAccessor onDesrialized)
 {
     return(new SerializableType.DescribedMapType(serializer, type, baseType, descriptorName, descriptorCode, members, knownTypes, onDesrialized));
 }
Beispiel #10
0
 public DescribedMapType(AmqpContractSerializer serializer, Type type, SerializableType baseType, string descriptorName, ulong?descriptorCode, SerialiableMember[] members, Dictionary <Type, SerializableType> knownTypes, MethodAccessor onDesrialized) : base(serializer, type, baseType, descriptorName, descriptorCode, members, knownTypes, onDesrialized)
 {
     this.membersMap = new Dictionary <string, SerialiableMember>();
     SerialiableMember[] serialiableMemberArray = members;
     for (int i = 0; i < (int)serialiableMemberArray.Length; i++)
     {
         SerialiableMember serialiableMember = serialiableMemberArray[i];
         this.membersMap.Add(serialiableMember.Name, serialiableMember);
     }
 }
Beispiel #11
0
 public DescribedListType(AmqpContractSerializer serializer, Type type, SerializableType baseType, string descriptorName, ulong?descriptorCode, SerialiableMember[] members, Dictionary <Type, SerializableType> knownTypes, MethodAccessor onDesrialized) : base(serializer, type, baseType, descriptorName, descriptorCode, members, knownTypes, onDesrialized)
 {
 }
        private SerializableType CompileType(Type type, bool describedOnly)
        {
            int valueOrDefault;

            object[] customAttributes = type.GetCustomAttributes(typeof(AmqpContractAttribute), false);
            if ((int)customAttributes.Length == 0)
            {
                if (describedOnly)
                {
                    return(null);
                }
                return(this.CompileNonContractTypes(type));
            }
            AmqpContractAttribute amqpContractAttribute = (AmqpContractAttribute)customAttributes[0];
            SerializableType      serializableType      = null;

            if (type.BaseType != typeof(object))
            {
                serializableType = this.CompileType(type.BaseType, true);
                if (serializableType != null)
                {
                    if (serializableType.Encoding != amqpContractAttribute.Encoding)
                    {
                        throw new SerializationException(SRAmqp.AmqpEncodingTypeMismatch(type.Name, amqpContractAttribute.Encoding, type.BaseType.Name, serializableType.Encoding));
                    }
                    this.customTypeCache.TryAdd(type.BaseType, serializableType);
                }
            }
            string name         = amqpContractAttribute.Name;
            ulong? internalCode = amqpContractAttribute.InternalCode;

            if (name == null && !internalCode.HasValue)
            {
                name = type.FullName;
            }
            List <SerialiableMember> serialiableMembers = new List <SerialiableMember>();

            if (amqpContractAttribute.Encoding == EncodingType.List && serializableType != null)
            {
                serialiableMembers.AddRange(serializableType.Members);
            }
            int count = serialiableMembers.Count + 1;

            MemberInfo[]   members        = type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            MethodAccessor methodAccessor = null;

            MemberInfo[] memberInfoArray = members;
            for (int i = 0; i < (int)memberInfoArray.Length; i++)
            {
                MemberInfo memberInfo = memberInfoArray[i];
                if (memberInfo.DeclaringType == type)
                {
                    if (memberInfo.MemberType == MemberTypes.Field || memberInfo.MemberType == MemberTypes.Property)
                    {
                        object[] objArray = memberInfo.GetCustomAttributes(typeof(AmqpMemberAttribute), true);
                        if ((int)objArray.Length == 1)
                        {
                            AmqpMemberAttribute amqpMemberAttribute = (AmqpMemberAttribute)objArray[0];
                            SerialiableMember   serialiableMember   = new SerialiableMember()
                            {
                                Name = amqpMemberAttribute.Name ?? memberInfo.Name
                            };
                            SerialiableMember serialiableMember1 = serialiableMember;
                            int?internalOrder = amqpMemberAttribute.InternalOrder;
                            if (internalOrder.HasValue)
                            {
                                valueOrDefault = internalOrder.GetValueOrDefault();
                            }
                            else
                            {
                                valueOrDefault = count;
                                count          = valueOrDefault + 1;
                            }
                            serialiableMember1.Order    = valueOrDefault;
                            serialiableMember.Mandatory = amqpMemberAttribute.Mandatory;
                            serialiableMember.Accessor  = MemberAccessor.Create(memberInfo, true);
                            serialiableMember.Type      = this.GetType((memberInfo.MemberType == MemberTypes.Field ? ((FieldInfo)memberInfo).FieldType : ((PropertyInfo)memberInfo).PropertyType));
                            serialiableMembers.Add(serialiableMember);
                        }
                    }
                    else if (memberInfo.MemberType == MemberTypes.Method && (int)memberInfo.GetCustomAttributes(typeof(OnDeserializedAttribute), false).Length == 1)
                    {
                        methodAccessor = MethodAccessor.Create((MethodInfo)memberInfo);
                    }
                }
            }
            if (amqpContractAttribute.Encoding == EncodingType.List)
            {
                serialiableMembers.Sort(AmqpContractSerializer.MemberOrderComparer.Instance);
                int order = -1;
                foreach (SerialiableMember serialiableMember2 in serialiableMembers)
                {
                    if (order > 0 && serialiableMember2.Order == order)
                    {
                        throw new SerializationException(SRAmqp.AmqpDuplicateMemberOrder(order, type.Name));
                    }
                    order = serialiableMember2.Order;
                }
            }
            SerialiableMember[] array = serialiableMembers.ToArray();
            Dictionary <Type, SerializableType> types = null;

            object[] customAttributes1 = type.GetCustomAttributes(typeof(KnownTypeAttribute), false);
            for (int j = 0; j < (int)customAttributes1.Length; j++)
            {
                KnownTypeAttribute knownTypeAttribute = (KnownTypeAttribute)customAttributes1[j];
                if ((int)knownTypeAttribute.Type.GetCustomAttributes(typeof(AmqpContractAttribute), false).Length > 0)
                {
                    if (types == null)
                    {
                        types = new Dictionary <Type, SerializableType>();
                    }
                    types.Add(knownTypeAttribute.Type, null);
                }
            }
            if (amqpContractAttribute.Encoding == EncodingType.List)
            {
                return(SerializableType.CreateDescribedListType(this, type, serializableType, name, internalCode, array, types, methodAccessor));
            }
            if (amqpContractAttribute.Encoding != EncodingType.Map)
            {
                throw new NotSupportedException(amqpContractAttribute.Encoding.ToString());
            }
            return(SerializableType.CreateDescribedMapType(this, type, serializableType, name, internalCode, array, types, methodAccessor));
        }