Example #1
0
        SerializableType CompileCollectionTypes(Type type)
        {
            MemberAccessor keyAccessor   = null;
            MemberAccessor valueAccessor = null;
            MethodAccessor addAccess     = null;
            Type           itemType      = null;

            foreach (Type it in type.GetInterfaces())
            {
                if (it.IsGenericType())
                {
                    Type genericTypeDef = it.GetGenericTypeDefinition();
                    if (genericTypeDef == typeof(IDictionary <,>))
                    {
                        Type[] argTypes = it.GetGenericArguments();
                        itemType      = typeof(KeyValuePair <,>).MakeGenericType(argTypes);
                        keyAccessor   = MemberAccessor.Create(itemType.GetProperty("Key"), false);
                        valueAccessor = MemberAccessor.Create(itemType.GetProperty("Value"), false);
                        addAccess     = MethodAccessor.Create(type.GetMethod("Add", argTypes));

                        return(SerializableType.CreateGenericMapType(this, type, keyAccessor, valueAccessor, addAccess));
                    }
                    else if (genericTypeDef == typeof(IList <>))
                    {
                        Type[] argTypes = it.GetGenericArguments();
                        itemType  = argTypes[0];
                        addAccess = MethodAccessor.Create(type.GetMethod("Add", argTypes));

                        return(SerializableType.CreateGenericListType(this, type, itemType, addAccess));
                    }
                }
            }

            return(null);
        }
        bool TryCreateMethodAccessor(MethodInfo methodInfo, Type attributeType, out MethodAccessor methodAccessor)
        {
            object[] memberAttributes = methodInfo.GetCustomAttributes(attributeType, false);
            if (memberAttributes.Length == 1)
            {
                methodAccessor = MethodAccessor.Create((MethodInfo)methodInfo);
                return(true);
            }

            methodAccessor = null;
            return(false);
        }
Example #3
0
        bool TryCreateMethodAccessor <T>(MethodInfo methodInfo, out MethodAccessor methodAccessor) where T : Attribute
        {
            T memberAttribute = methodInfo.GetCustomAttribute <T>(false);

            if (memberAttribute != null)
            {
                methodAccessor = MethodAccessor.Create((MethodInfo)methodInfo);
                return(true);
            }

            methodAccessor = null;
            return(false);
        }
Example #4
0
        SerializableType CompileCollectionTypes(Type type, HashSet <Type> pendingTypes)
        {
            MemberAccessor keyAccessor   = null;
            MemberAccessor valueAccessor = null;
            MethodAccessor addAccess     = null;

            if (type.IsArray)
            {
                // array of custom types. encode it as list
                var itemType = type.GetElementType();
                var listType = this.GetOrCompileType(typeof(List <>).MakeGenericType(itemType), false, pendingTypes);
                return(SerializableType.CreateArrayType(this, type, itemType, listType));
            }

            foreach (Type it in type.GetInterfaces())
            {
                if (it.IsGenericType())
                {
                    Type genericTypeDef = it.GetGenericTypeDefinition();
                    if (genericTypeDef == typeof(IDictionary <,>))
                    {
                        Type[] argTypes = it.GetGenericArguments();
                        var    itemType = typeof(KeyValuePair <,>).MakeGenericType(argTypes);
                        keyAccessor   = MemberAccessor.Create(itemType.GetProperty("Key"), false);
                        valueAccessor = MemberAccessor.Create(itemType.GetProperty("Value"), false);
                        addAccess     = MethodAccessor.Create(type.GetMethod("Add", argTypes));
                        var keyType   = this.GetOrCompileType(keyAccessor.Type, false, pendingTypes);
                        var valueType = this.GetOrCompileType(valueAccessor.Type, false, pendingTypes);

                        return(SerializableType.CreateGenericMapType(this, type, keyType,
                                                                     valueType, keyAccessor, valueAccessor, addAccess));
                    }
                    else if (genericTypeDef == typeof(IList <>))
                    {
                        Type[] argTypes = it.GetGenericArguments();
                        var    itemType = this.GetOrCompileType(argTypes[0], false, pendingTypes);
                        addAccess = MethodAccessor.Create(type.GetMethod("Add", argTypes));

                        return(SerializableType.CreateGenericListType(this, type, itemType, addAccess));
                    }
                }
            }

            return(null);
        }
Example #5
0
        SerializableType CreateContractType(AmqpContract contract)
        {
            Type   type           = contract.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 = GetType(memberType);
            }

            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);
            }

            Dictionary <Type, SerializableType> knownTypes = null;

            if (contract.Provides != null)
            {
                knownTypes = new Dictionary <Type, SerializableType>();
                for (int i = 0; i < contract.Provides.Length; i++)
                {
                    // KnownType compilation is delayed and non-recursive to avoid circular references
                    knownTypes.Add(contract.Provides[i], null);
                }
            }

            if (contract.Attribute.Encoding == EncodingType.List)
            {
                return(SerializableType.CreateDescribedListType(this, type, baseType, descriptorName,
                                                                descriptorCode, members, knownTypes, serializationCallbacks));
            }
            else if (contract.Attribute.Encoding == EncodingType.Map)
            {
                return(SerializableType.CreateDescribedMapType(this, type, baseType, descriptorName,
                                                               descriptorCode, members, knownTypes, serializationCallbacks));
            }
            else if (contract.Attribute.Encoding == EncodingType.SimpleMap)
            {
                return(SerializableType.CreateDescribedSimpleMapType(this, type, baseType, members, serializationCallbacks));
            }
            else if (contract.Attribute.Encoding == EncodingType.SimpleList)
            {
                return(SerializableType.CreateDescribedSimpleListType(this, type, baseType, members, serializationCallbacks));
            }
            else
            {
                throw new NotSupportedException(contract.Attribute.Encoding.ToString());
            }
        }
Example #6
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);
        }
        SerializableType CompileType(Type type, bool describedOnly)
        {
            object[] typeAttributes = type.GetCustomAttributes(typeof(AmqpContractAttribute), false);
            if (typeAttributes.Length == 0)
            {
                if (describedOnly)
                {
                    return(null);
                }
                else
                {
                    return(CompileNonContractTypes(type));
                }
            }

            AmqpContractAttribute contractAttribute = (AmqpContractAttribute)typeAttributes[0];
            SerializableType      baseType          = null;

            if (type.BaseType != typeof(object))
            {
                baseType = this.CompileType(type.BaseType, true);
                if (baseType != null)
                {
                    if (baseType.Encoding != contractAttribute.Encoding)
                    {
                        throw new SerializationException(
                                  Fx.Format("{0}.Encoding ({1}) is different from {2}.Encoding ({3})",
                                            type.Name, contractAttribute.Encoding, type.BaseType.Name, baseType.Encoding));
                    }

                    this.typeCache[type.BaseType] = baseType;
                }
            }

            string descriptorName = contractAttribute.Name;
            ulong? descriptorCode = contractAttribute.InternalCode;

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

            List <SerialiableMember> memberList = new List <SerialiableMember>();

            if (contractAttribute.Encoding == EncodingType.List && baseType != null)
            {
                memberList.AddRange(baseType.Members);
            }

            int lastOrder = memberList.Count + 1;

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

            foreach (MemberInfo memberInfo in memberInfos)
            {
                if (memberInfo.DeclaringType != type)
                {
                    continue;
                }

                if (memberInfo.MemberType == MemberTypes.Field ||
                    memberInfo.MemberType == MemberTypes.Property)
                {
                    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.Accessor = MemberAccessor.Create(memberInfo, true);

                    // 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);
                }
                else if (memberInfo.MemberType == MemberTypes.Method)
                {
                    object[] memberAttributes = memberInfo.GetCustomAttributes(typeof(OnDeserializedAttribute), false);
                    if (memberAttributes.Length == 1)
                    {
                        onDeserialized = MethodAccessor.Create((MethodInfo)memberInfo);
                    }
                }
            }

            if (contractAttribute.Encoding == EncodingType.List)
            {
                memberList.Sort(MemberOrderComparer.Instance);
                int order = -1;
                foreach (SerialiableMember member in memberList)
                {
                    if (order > 0 && member.Order == order)
                    {
                        throw new SerializationException(Fx.Format("Duplicate Order {0} detected in {1}", order, type.Name));
                    }

                    order = member.Order;
                }
            }

            SerialiableMember[] members = memberList.ToArray();

            Dictionary <Type, SerializableType> knownTypes = null;

            foreach (object o in type.GetCustomAttributes(typeof(AmqpProvidesAttribute), false))
            {
                AmqpProvidesAttribute knownAttribute = (AmqpProvidesAttribute)o;
                if (knownAttribute.Type.GetCustomAttributes(typeof(AmqpContractAttribute), false).Length > 0)
                {
                    if (knownTypes == null)
                    {
                        knownTypes = new Dictionary <Type, SerializableType>();
                    }

                    // KnownType compilation is delayed and non-recursive to avoid circular references
                    knownTypes.Add(knownAttribute.Type, null);
                }
            }

            if (contractAttribute.Encoding == EncodingType.List)
            {
                return(SerializableType.CreateDescribedListType(this, type, baseType, descriptorName,
                                                                descriptorCode, members, knownTypes, onDeserialized));
            }
            else if (contractAttribute.Encoding == EncodingType.Map)
            {
                return(SerializableType.CreateDescribedMapType(this, type, baseType, descriptorName,
                                                               descriptorCode, members, knownTypes, onDeserialized));
            }
            else
            {
                throw new NotSupportedException(contractAttribute.Encoding.ToString());
            }
        }