示例#1
0
        private static Func <Action <T, JsonSerializerHandler> > TypeIsDictionaryInterface(Type t)
        {
            if (t == typeof(IDictionary))
            {
                return(() => (obj, handler) => SpecialConditions.WriteDictionary((IDictionary)obj, handler));
            }

            if (t.IsGenericType)
            {
                Type[] args = t.GetGenericArguments();
                if (args.Length != 2)
                {
                    return(null);
                }
                var genericTypeDefinition = t.GetGenericTypeDefinition();
                var keyType   = args[0];
                var valueType = args[1];

                if (genericTypeDefinition == typeof(IDictionary <,>) || genericTypeDefinition == typeof(IReadOnlyDictionary <,>))
                {
                    return(() => IsWrongKeyType(keyType) ?? BuildFactory.Create <T>(SerializerBuildTypeEnum.IDictionaryGeneric));
                }
            }

            return(null);
        }
示例#2
0
        private static Func <Action <T, JsonSerializerHandler> > TypeIsDictionaryType(Type t)
        {
            if (t.IsInterface)
            {
                return(null);
            }

            var intserfaces = t.GetInterfaces();

            bool hasIDictionaryGeneric  = false;
            bool hasIDictionary         = false;
            Type iDictionaryGenericType = null;

            Type keyType   = null;
            Type valueType = null;

            foreach (var item in intserfaces)
            {
                if (item.IsGenericType)
                {
                    var genericTypeDefinition = item.GetGenericTypeDefinition();
                    if (genericTypeDefinition == typeof(IDictionary <,>))
                    {
                        iDictionaryGenericType = item;
                        hasIDictionaryGeneric  = true;
                        keyType   = item.GetGenericArguments()[0];
                        valueType = item.GetGenericArguments()[1];
                    }
                }
                if (item == typeof(IDictionary))
                {
                    hasIDictionary = true;
                }
            }

            if (hasIDictionaryGeneric)
            {
                var ctor = t.GetDefaultNoArgCtorOrAppointTypeCtor(iDictionaryGenericType);
                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (TypeIsDictionary(firstCtor.ParameterType) != null)
                            {
                                return(() => IsWrongKeyType(keyType) ?? BuildFactory.Create <T>(SerializerBuildTypeEnum.IDictionaryGeneric));
                            }
                        }
                    }
                }
                else if (ctor.GetParameters().Length == 0)
                {
                    return(() => IsWrongKeyType(keyType) ?? BuildFactory.Create <T>(SerializerBuildTypeEnum.IDictionaryGeneric));
                }
                else
                {
                    return(() => IsWrongKeyType(keyType) ?? BuildFactory.Create <T>(SerializerBuildTypeEnum.IDictionaryGeneric));
                }
            }
            if (hasIDictionary)
            {
                var ctor = t.GetDefaultNoArgCtorOrAppointTypeCtor(typeof(IDictionary));

                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (TypeIsDictionary(firstCtor.ParameterType) != null)
                            {
                                return(() => (obj, handler) => SpecialConditions.WriteDictionary((IDictionary)obj, handler));
                            }
                        }
                    }
                }
                else if (ctor.GetParameters().Length == 0)
                {
                    return(() => (obj, handler) => SpecialConditions.WriteDictionary((IDictionary)obj, handler));
                }
                else
                {
                    return(() => (obj, handler) => SpecialConditions.WriteDictionary((IDictionary)obj, handler));
                }
            }


            return(null);
        }