예제 #1
0
        private static IImporter FindCompatibleImporter(Type type)
        {
            Debug.Assert(type != null);

            if (typeof(IJsonImportable).IsAssignableFrom(type))
            {
                return(new ImportAwareImporter(type));
            }

            if (type.IsArray && type.GetArrayRank() == 1)
            {
                return(new ArrayImporter(type));
            }

            if (type.IsEnum)
            {
                return(new EnumImporter(type));
            }

            #if !NET_1_0 && !NET_1_1
            if (Reflector.IsConstructionOfNullable(type))
            {
                return(new NullableImporter(type));
            }

            bool isGenericList       = Reflector.IsConstructionOfGenericTypeDefinition(type, typeof(IList <>));
            bool isGenericCollection = !isGenericList && Reflector.IsConstructionOfGenericTypeDefinition(type, typeof(ICollection <>));
            bool isSequence          = !isGenericCollection && (type == typeof(IEnumerable) || Reflector.IsConstructionOfGenericTypeDefinition(type, typeof(IEnumerable <>)));

            if (isGenericList || isGenericCollection || isSequence)
            {
                Type itemType = type.IsGenericType
                              ? type.GetGenericArguments()[0]
                              : typeof(object);
                Type importerType = typeof(CollectionImporter <,>).MakeGenericType(new Type[] { type, itemType });
                return((IImporter)Activator.CreateInstance(importerType, new object[] { isSequence }));
            }

            if (Reflector.IsConstructionOfGenericTypeDefinition(type, typeof(IDictionary <,>)))
            {
                return((IImporter)Activator.CreateInstance(typeof(DictionaryImporter <,>).MakeGenericType(type.GetGenericArguments())));
            }
            #endif

            #if !NET_1_0 && !NET_1_1 && !NET_2_0
            if (Reflector.IsConstructionOfGenericTypeDefinition(type, typeof(ISet <>)))
            {
                Type[] typeArguments = type.GetGenericArguments();
                Type   hashSetType   = typeof(HashSet <>).MakeGenericType(typeArguments);
                return((IImporter)Activator.CreateInstance(typeof(CollectionImporter <, ,>).MakeGenericType(new Type[] { hashSetType, type, typeArguments[0] })));
            }

            if (Reflector.IsTupleFamily(type))
            {
                return(new TupleImporter(type));
            }
            #endif

            if ((type.IsPublic || type.IsNestedPublic) &&
                !type.IsPrimitive &&
                (type.IsValueType || type.GetConstructor(Type.EmptyTypes) != null))
            {
                if (type.IsValueType)
                {
                    CustomTypeDescriptor logicalType = new CustomTypeDescriptor(type);

                    if (logicalType.GetProperties().Count > 0)
                    {
                        return(new ComponentImporter(type, logicalType));
                    }
                }
                else
                {
                    return(new ComponentImporter(type));
                }
            }

            return(null);
        }
예제 #2
0
        private IExporter FindCompatibleExporter(Type type)
        {
            Debug.Assert(type != null);

            if (typeof(IJsonExportable).IsAssignableFrom(type))
            {
                return(new ExportAwareExporter(type));
            }

            #if !NET_1_0 && !NET_1_1
            if (Reflector.IsConstructionOfNullable(type))
            {
                return(new NullableExporter(type));
            }
            #endif

            #if !NET_1_0 && !NET_1_1 && !NET_2_0
            if (Reflector.IsTupleFamily(type))
            {
                return(new TupleExporter(type));
            }
            #endif

            if (type.IsClass && type != typeof(object))
            {
                IExporter exporter = FindBaseExporter(type.BaseType, type);
                if (exporter != null)
                {
                    return(exporter);
                }
            }

            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                return(new DictionaryExporter(type));
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return(new EnumerableExporter(type));
            }

            if ((type.IsPublic || type.IsNestedPublic) &&
                !type.IsPrimitive && !type.IsEnum &&
                (type.IsValueType || type.GetConstructor(Type.EmptyTypes) != null))
            {
                if (type.IsValueType)
                {
                    CustomTypeDescriptor descriptor = new CustomTypeDescriptor(type);

                    if (descriptor.GetProperties().Count > 0)
                    {
                        return(new ComponentExporter(type, descriptor));
                    }
                }
                else
                {
                    return(new ComponentExporter(type));
                }
            }

            CustomTypeDescriptor anonymousClass = CustomTypeDescriptor.TryCreateForAnonymousClass(type);
            if (anonymousClass != null)
            {
                return(new ComponentExporter(type, anonymousClass));
            }

            return(new StringExporter(type));
        }