Example #1
0
        static SerializableType CompileType(Type type)
        {
            // at this point, type is a composite type
            object[] typeAttributes = type.GetCustomAttributes(typeof(AmqpContractAttribute), true);
            if (typeAttributes.Length != 1)
            {
                throw new NotSupportedException(type.FullName);
            }

            AmqpContractAttribute contractAttribute = (AmqpContractAttribute)typeAttributes[0];
            string descriptorName = contractAttribute.Name;
            ulong? descriptorCode = contractAttribute.InternalCode;

            if (descriptorName == null && descriptorCode == null)
            {
                descriptorName = type.FullName;
            }

            int lastOrder = 0;
            SortedList <int, SerialiableMember> memberList = new SortedList <int, SerialiableMember>();

            MemberInfo[] memberInfos = type.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (MemberInfo memberInfo in memberInfos)
            {
                if (memberInfo.DeclaringType != type ||
                    (memberInfo.MemberType != MemberTypes.Field &&
                     memberInfo.MemberType != MemberTypes.Property))
                {
                    continue;
                }

                object[] memberAttributes = memberInfo.GetCustomAttributes(typeof(AmqpMemberAttribute), true);
                if (memberAttributes.Length != 1)
                {
                    continue;
                }

                AmqpMemberAttribute attribute = (AmqpMemberAttribute)memberAttributes[0];

                SerialiableMember member = new SerialiableMember();
                member.Name      = attribute.Name ?? memberInfo.Name;
                member.Order     = attribute.InternalOrder ?? lastOrder;
                member.Mandatory = attribute.Mandatory;
                member.Accessor  = MemberAccessor.Create(memberInfo);

                // This will recursively resolve member types
                Type memberType = memberInfo.MemberType == MemberTypes.Field ? ((FieldInfo)memberInfo).FieldType : ((PropertyInfo)memberInfo).PropertyType;
                member.Type = GetType(memberType);

                memberList.Add(member.Order, member);
                lastOrder = member.Order >= lastOrder ? member.Order + 1 : lastOrder + 1;
            }

            SerialiableMember[] members = new SerialiableMember[memberList.Count];
            for (int i = 0; i < memberList.Count; ++i)
            {
                members[i] = memberList[i];
            }

            SerializableType serializableType = SerializableType.Create(type, descriptorName, descriptorCode, members);

            return(serializableType);
        }
        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));
        }