Example #1
0
        SerializableType CreateContractType(AmqpContract contract, HashSet <Type> pendingTypes)
        {
            Type type = contract.Type;

            if (pendingTypes.Contains(type))
            {
                return(SerializableType.CreateDelegatingType(this, type));
            }

            pendingTypes.Add(type);
            string descriptorName = contract.Attribute.Name;
            ulong? descriptorCode = contract.Attribute.InternalCode;

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

            SerializableMember[] members = new SerializableMember[contract.Members.Length];
            for (int i = 0; i < contract.Members.Length; i++)
            {
                SerializableMember member = new SerializableMember();
                members[i] = member;

                AmqpMember amqpMember = contract.Members[i];
                member.Name     = amqpMember.Name;
                member.Order    = amqpMember.Order;
                member.Accessor = MemberAccessor.Create(amqpMember.Info, true);

                // This will recursively resolve member types
                Type memberType = amqpMember.Info is FieldInfo ?
                                  ((FieldInfo)amqpMember.Info).FieldType :
                                  ((PropertyInfo)amqpMember.Info).PropertyType;
                member.Type = GetOrCompileType(memberType, false, pendingTypes);
            }

            MethodAccessor[] serializationCallbacks = new MethodAccessor[]
            {
                contract.Serializing == null ? null : MethodAccessor.Create(contract.Serializing),
                contract.Serialized == null ? null : MethodAccessor.Create(contract.Serialized),
                contract.Deserializing == null ? null : MethodAccessor.Create(contract.Deserializing),
                contract.Deserialized == null ? null : MethodAccessor.Create(contract.Deserialized)
            };

            SerializableType baseType = null;

            if (contract.BaseContract != null)
            {
                baseType = this.CreateContractType(contract.BaseContract, pendingTypes);
            }

            SerializableType[] knownTypes = null;
            if (contract.Provides != null)
            {
                knownTypes = new SerializableType[contract.Provides.Length];
                for (int i = 0; i < contract.Provides.Length; i++)
                {
                    knownTypes[i] = this.GetOrCompileType(contract.Provides[i], true, pendingTypes);
                }
            }

            SerializableType result;

            if (contract.Attribute.Encoding == EncodingType.List)
            {
                result = SerializableType.CreateDescribedListType(this, type, baseType, descriptorName,
                                                                  descriptorCode, members, knownTypes, serializationCallbacks);
            }
            else if (contract.Attribute.Encoding == EncodingType.Map)
            {
                result = SerializableType.CreateDescribedMapType(this, type, baseType, descriptorName,
                                                                 descriptorCode, members, knownTypes, serializationCallbacks);
            }
            else if (contract.Attribute.Encoding == EncodingType.SimpleMap)
            {
                result = SerializableType.CreateDescribedSimpleMapType(this, type, baseType, members, serializationCallbacks);
            }
            else if (contract.Attribute.Encoding == EncodingType.SimpleList)
            {
                result = SerializableType.CreateDescribedSimpleListType(this, type, baseType, members, serializationCallbacks);
            }
            else
            {
                throw new NotSupportedException(contract.Attribute.Encoding.ToString());
            }

            pendingTypes.Remove(type);
            return(result);
        }