예제 #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);
        }
예제 #2
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);
        }