Exemple #1
0
 private static Action <T, JsonSerializerHandler> IsArray(Type t)
 {
     if (t.IsArray)
     {
         if (t.GetArrayRank() == 1)
         {
             return(BuildFactory.Create <T>(SerializerBuildTypeEnum.IListGeneric));
         }
         else
         {
             return((obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
         }
     }
     return(null);
 }
Exemple #2
0
        private static Func <Action <T, JsonSerializerHandler> > TypeIsCollectionInterface(Type t)
        {
            if (t == typeof(IEnumerable) || t == typeof(ICollection) || t == typeof(IList))
            {
                return(() => (obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
            }

            if (t.IsGenericType)
            {
                var genericTypeDefinition = t.GetGenericTypeDefinition();
                var arrayItemType         = t.GetGenericArguments()[0];

                if (genericTypeDefinition == typeof(IEnumerable <>) || genericTypeDefinition == typeof(IList <>) || genericTypeDefinition == typeof(ICollection <>) || genericTypeDefinition == typeof(ISet <>) || genericTypeDefinition == typeof(IReadOnlyList <>) || genericTypeDefinition == typeof(IReadOnlyCollection <>))
                {
                    return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IEnumerableGeneric));
                }
            }

            return(null);
        }
Exemple #3
0
        private static Func <Action <T, JsonSerializerHandler> > TypeIsCollectionType(Type t)
        {
            if (t.IsInterface)
            {
                return(null);
            }

            var intserfaces = t.GetInterfaces();

            bool hasIEnumerableGeneric = false;
            bool hasICollectionGeneric = false;
            bool hasIListGeneric       = false;
            bool hasICollection        = false;
            bool hasIList = false;

            Type arrayItemType          = null;
            Type iCollectionGenericType = null;
            Type iEnumerableGenericType = null;
            Type iListGenericType       = null;

            foreach (var item in intserfaces)
            {
                if (item.IsGenericType)
                {
                    var genericTypeDefinition = item.GetGenericTypeDefinition();

                    if (genericTypeDefinition == typeof(IEnumerable <>))
                    {
                        hasIEnumerableGeneric  = true;
                        arrayItemType          = item.GetGenericArguments()[0];
                        iEnumerableGenericType = item;
                    }
                    else if (genericTypeDefinition == typeof(ICollection <>))
                    {
                        hasICollectionGeneric  = true;
                        arrayItemType          = item.GetGenericArguments()[0];
                        iCollectionGenericType = item;
                    }
                    else if (genericTypeDefinition == typeof(IList <>))
                    {
                        hasIListGeneric  = true;
                        arrayItemType    = item.GetGenericArguments()[0];
                        iListGenericType = item;
                    }
                }
                else if (item == typeof(ICollection))
                {
                    hasICollection = true;
                }
                else if (item == typeof(IList))
                {
                    hasIList = true;
                }
            }

            if (hasIListGeneric)
            {
                var ctor = t.GetDefaultNoArgCtorOrAppointTypeCtor(iListGenericType);
                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (firstCtor.ParameterType.IsArray || TypeIsCollection(firstCtor.ParameterType) != null)
                            {
                                return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IListGeneric));
                            }
                        }
                    }
                }
                else if (ctor.GetParameters().Length == 0)
                {
                    return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IListGeneric));
                }
                else
                {
                    return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IListGeneric));
                }
            }


            if (hasICollectionGeneric)
            {
                var ctor = t.GetDefaultNoArgCtorOrAppointTypeCtor(iCollectionGenericType);
                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (firstCtor.ParameterType.IsArray || TypeIsCollection(firstCtor.ParameterType) != null)
                            {
                                return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IEnumerableGeneric));
                            }
                        }
                    }
                }
                else if (ctor.GetParameters().Length == 0)
                {
                    return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IEnumerableGeneric));
                }
                else
                {
                    return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IEnumerableGeneric));
                }
            }

            if (hasIEnumerableGeneric && hasICollection)
            {
                var ctor = t.GetAppointTypeCtor(iEnumerableGenericType);
                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (firstCtor.ParameterType.IsArray || TypeIsCollection(firstCtor.ParameterType) != null)
                            {
                                return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IEnumerableGeneric));
                            }
                        }
                    }
                }
                else
                {
                    return(() => BuildFactory.Create <T>(SerializerBuildTypeEnum.IEnumerableGeneric));
                }
            }

            if (hasIList)
            {
                var ctor = t.GetDefaultNoArgCtorOrAppointTypeCtor(typeof(IList));
                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (firstCtor.ParameterType.IsArray || TypeIsCollection(firstCtor.ParameterType) != null)
                            {
                                return(() => (obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
                            }
                        }
                    }
                }
                else if (ctor.GetParameters().Length == 0)
                {
                    return(() => (obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
                }
                else
                {
                    return(() => (obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
                }
            }

            if (hasICollection)
            {
                var ctor = t.GetAppointTypeCtor(typeof(ICollection));
                if (ctor == null)
                {
                    foreach (var item in t.GetConstructors())
                    {
                        if (item.GetParameters().Length == 1)
                        {
                            var firstCtor = item.GetParameters()[0];
                            if (firstCtor.ParameterType.IsArray || TypeIsCollection(firstCtor.ParameterType) != null)
                            {
                                return(() => (obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
                            }
                        }
                    }
                }
                else
                {
                    return(() => (obj, handler) => SpecialConditions.WriteCollection((IEnumerable)obj, handler));
                }
            }


            return(null);
        }