Esempio n. 1
0
        MemberCache(MemberInfo memberInfo, Type memberType, string name)
        {
            MemberName = name;
            MemberType = memberType;
            MemberInfo = memberInfo;
            JsonDataType dt = Reflection.GetJsonDataType(memberType);

            DeserializeMethod = JsonDeserializer.GetReadJsonMethod(memberType);
            SerializeMethod   = JsonSerializer.GetWriteJsonMethod(memberType);

            if (dt == JsonDataType.Array || dt == JsonDataType.MultiDimensionalArray)
            {
                ElementType = memberType.GetElementType();
            }

            IsValueType  = memberType.IsValueType;
            IsStruct     = (IsValueType && !memberType.IsPrimitive && !memberType.IsEnum && typeof(decimal).Equals(memberType) == false);
            IsClass      = memberType.IsClass;
            IsCollection = typeof(ICollection).IsAssignableFrom(memberType) && typeof(byte[]).Equals(memberType) == false;
            if (memberType.IsGenericType)
            {
                ElementType = memberType.GetGenericArguments()[0];
                IsNullable  = memberType.GetGenericTypeDefinition().Equals(typeof(Nullable <>));
            }
            if (IsValueType)
            {
                ChangeType = IsNullable ? ElementType : memberType;
            }
            JsonDataType = dt;
        }
Esempio n. 2
0
        internal ReflectionCache(Type type)
        {
            Type         = type;
            TypeName     = type.FullName;
            AssemblyName = type.AssemblyQualifiedName;

            JsonDataType      = Reflection.GetJsonDataType(type);
            SerializeMethod   = JsonSerializer.GetWriteJsonMethod(type);
            DeserializeMethod = JsonDeserializer.GetReadJsonMethod(type);

            if (JsonDataType == JsonDataType.Enum)
            {
                IsFlaggedEnum = AttributeHelper.HasAttribute <FlagsAttribute> (type, false);
                return;
            }

            if (type.IsArray)
            {
                ArgumentTypes = new Type[] { type.GetElementType() };
                CommonType    = type.GetArrayRank() == 1 ? ComplexType.Array : ComplexType.MultiDimensionalArray;
            }
            else
            {
                var t = type;
                if (t.IsGenericType == false)
                {
                    while ((t = t.BaseType) != null)
                    {
                        if (t.IsGenericType)
                        {
                            break;
                        }
                    }
                }
                if (t != null)
                {
                    ArgumentTypes = t.GetGenericArguments();
                    var gt = t.GetGenericTypeDefinition();
                    if (gt.Equals(typeof(Dictionary <,>)))
                    {
                        CommonType = ComplexType.Dictionary;
                    }
                    else if (gt.Equals(typeof(List <>)))
                    {
                        CommonType = ComplexType.List;
                    }
                    else if (gt.Equals(typeof(Nullable <>)))
                    {
                        CommonType      = ComplexType.Nullable;
                        SerializeMethod = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]);
                    }
                }
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                if (typeof(Array).IsAssignableFrom(type) == false)
                {
                    AppendItem = Reflection.CreateWrapperMethod <AddCollectionItem> (Reflection.FindMethod(type, "Add", new Type[1] {
                        null
                    }));
                }
                if (ArgumentTypes != null && ArgumentTypes.Length == 1)
                {
                    ItemSerializer   = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]);
                    ItemDeserializer = JsonDeserializer.GetReadJsonMethod(ArgumentTypes[0]);
                }
            }
            if (ArgumentTypes != null)
            {
                ArgumentReflections = new ReflectionCache[ArgumentTypes.Length];
            }
            if (CommonType != ComplexType.Array &&
                CommonType != ComplexType.MultiDimensionalArray &&
                CommonType != ComplexType.Nullable)
            {
                var t = type;
                if (type.IsNested == false && type.IsPublic == false)
                {
                    ConstructorInfo |= ConstructorTypes.NonPublic;
                }
                else
                {
                    while (t != null && t.IsNested)
                    {
                        if (t.IsNestedPublic == false)
                        {
                            ConstructorInfo |= ConstructorTypes.NonPublic;
                        }
                        t = t.DeclaringType;
                    }
                }
                if (type.IsClass || type.IsValueType)
                {
                    Constructor = Reflection.CreateConstructorMethod(type, type.IsVisible == false || typeof(DatasetSchema).Equals(type));
                    if (Constructor != null && Constructor.Method.IsPublic == false)
                    {
                        ConstructorInfo |= ConstructorTypes.NonPublic;
                    }
                    if (Constructor == null)
                    {
                        var c = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                        if (c != null && c.Length > 0)
                        {
                            ConstructorInfo |= ConstructorTypes.Parametric;
                        }
                    }

                    Members = Reflection.GetMembers(type);
                }
            }
            //if (typeof (IEnumerable).IsAssignableFrom (type)) {
            //	return;
            //}
            //if (JsonDataType != JsonDataType.Undefined) {
            //	return;
            //}
        }