TryCreateForAnonymousClass() public static method

public static TryCreateForAnonymousClass ( Type type ) : CustomTypeDescriptor
type System.Type
return CustomTypeDescriptor
Example #1
0
        IExporter FindCompatibleExporter(Type type)
        {
            Debug.Assert(type != null);

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

            if (Reflector.IsConstructionOfNullable(type))
            {
                return(new NullableExporter(type));
            }

            if (Reflector.IsValueTupleFamily(type))
            {
                return(new ValueTupleExporter(type));
            }

            if (Reflector.IsTupleFamily(type))
            {
                return(new TupleExporter(type));
            }

            if (type.IsClass && type != typeof(object))
            {
                var 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.GetConstructors().Length > 0))
            {
                return(new ComponentExporter(type));
            }

            var anonymousClass = CustomTypeDescriptor.TryCreateForAnonymousClass(type);

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

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

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

            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));
        }
        public void AnonymousClassPropertiesExcepted()
        {
            var anon = CustomTypeDescriptor.TryCreateForAnonymousClass(typeof(AnonymousThing <string>));

            Assert.IsNotNull(anon);
            var properties = anon.GetProperties();

            Assert.AreEqual(1, properties.Count);
            var property = properties[0];

            Assert.AreEqual("Value", property.Name);
            Assert.IsTrue(property.IsReadOnly);
        }
Example #4
0
        public void AnonymousClassPropertiesExcepted()
        {
#if NET_1_0 || NET_1_1
            CustomTypeDescriptor anon = CustomTypeDescriptor.TryCreateForAnonymousClass(typeof(AnonymousThing));
#else
            CustomTypeDescriptor anon = CustomTypeDescriptor.TryCreateForAnonymousClass(typeof(AnonymousThing <string>));
#endif
            Assert.IsNotNull(anon);
            PropertyDescriptorCollection properties = anon.GetProperties();
            Assert.AreEqual(1, properties.Count);
            PropertyDescriptor property = properties[0];
            Assert.AreEqual("Value", property.Name);
            Assert.IsTrue(property.IsReadOnly);
        }
Example #5
0
        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 (Reflector.IsConstructionOfNullable(type))
            {
                return(new NullableImporter(type));
            }

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

            if (isGenericList || isGenericCollection || isSequence)
            {
                var itemType = type.IsGenericType
                              ? type.GetGenericArguments()[0]
                              : typeof(object);
                var importerType = typeof(CollectionImporter <,>).MakeGenericType(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())));
            }

            var genericDictionaryType = Reflector.FindConstructionOfGenericInterfaceDefinition(type, typeof(IDictionary <,>));

            if (genericDictionaryType != null)
            {
                var args2 = genericDictionaryType.GetGenericArguments();
                Debug.Assert(args2.Length == 2);
                var args3 = new Type[3];
                args3[0] = type;        // [ TDictionary, ... , ...    ]
                args2.CopyTo(args3, 1); // [ TDictionary, TKey, TValue ]
                return((IImporter)Activator.CreateInstance(typeof(DictionaryImporter <, ,>).MakeGenericType(args3)));
            }

            if (Reflector.IsConstructionOfGenericTypeDefinition(type, typeof(ISet <>)))
            {
                var typeArguments = type.GetGenericArguments();
                var hashSetType   = typeof(HashSet <>).MakeGenericType(typeArguments);
                return((IImporter)Activator.CreateInstance(typeof(CollectionImporter <, ,>).MakeGenericType(hashSetType, type, typeArguments[0])));
            }

            if (Reflector.IsValueTupleFamily(type))
            {
                return(new ValueTupleImporter(type));
            }

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

            if ((type.IsPublic || type.IsNestedPublic) &&
                !type.IsPrimitive &&
                (type.IsValueType || type.GetConstructors().Length > 0))
            {
                return(new ComponentImporter(type, new ObjectConstructor(type)));
            }

            var anonymousClass = CustomTypeDescriptor.TryCreateForAnonymousClass(type);

            if (anonymousClass != null)
            {
                return(new ComponentImporter(type, anonymousClass, new ObjectConstructor(type)));
            }

            return(null);
        }