Provides an ICustomTypeDescriptor implementation on top of the public read/write fields and properties of a given type.
Inheritance: System.ComponentModel.ICustomTypeDescriptor
示例#1
0
文件: JsonType.cs 项目: db48x/KeeFox
 public ICustomTypeDescriptor ToCustomType()
 {
     if (_customType == null)
         _customType = new CustomTypeDescriptor(_type, _members.ToArray(), _names.ToArray());
     
     return _customType;
 }
        public void ReadOnlyPropertyWithIncludeAttributeIsIncluded()
        {
            var thingType  = new CustomTypeDescriptor(typeof(ThingWithSpecialReadOnlyProperty));
            var properties = thingType.GetProperties();

            Assert.AreEqual(1, properties.Count);
        }
示例#3
0
 public void MembersWithIgnoreAttributeExcluded()
 {
     CustomTypeDescriptor thingType = new CustomTypeDescriptor(typeof(Thing));
     Assert.AreEqual(4, thingType.GetProperties().Count);
     Assert.IsNull(thingType.GetProperties().Find("Field2", true));
     Assert.IsNull(thingType.GetProperties().Find("Property2", true));
 }
示例#4
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));
        }
        public void MembersWithIgnoreAttributeExcluded()
        {
            var thingType  = new CustomTypeDescriptor(typeof(Thing));
            var properties = thingType.GetProperties();

            Assert.AreEqual(4, properties.Count);
            Assert.IsNull(properties.Find("Field2", true));
            Assert.IsNull(properties.Find("Property2", true));
        }
示例#6
0
            public ICustomTypeDescriptor ToCustomType()
            {
                if (_customType == null)
                {
                    _customType = new CustomTypeDescriptor(_type, _members.ToArray(), _names.ToArray());
                }

                return(_customType);
            }
        public void ImmutableClassPropertiesExpected()
        {
            var descriptor = new CustomTypeDescriptor(typeof(ImmutableThing));
            var properties = descriptor.GetProperties();

            Assert.AreEqual(2, properties.Count);
            Assert.IsNotNull(properties["field"]);
            Assert.IsNotNull(properties["property"]);
        }
示例#8
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 IndexerPropertyExcluded()
        {
            //
            // Exercises bug #11675
            // http://developer.berlios.de/bugs/?func=detailbug&bug_id=11675&group_id=4638
            //

            var thingType = new CustomTypeDescriptor(typeof(ThingWithIndexer));

            Assert.AreEqual(0, thingType.GetProperties().Count);
        }
        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);
        }
示例#11
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);
        }
示例#12
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 ((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);
        }
示例#13
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;
        }
示例#14
0
 public static PropertyDescriptor GetProperty1Property()
 {
     return(CustomTypeDescriptor.CreateProperty(typeof(Thing).GetProperty("Property1")));
 }
示例#15
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);
        }
示例#16
0
 public ICustomTypeDescriptor ToCustomType()
 {
     return(_customType
            ?? (_customType = new CustomTypeDescriptor(_type, _members.ToArray(), _names.ToArray())));
 }
示例#17
0
 void OnChanging()
 {
     _customType = null;
 }
示例#18
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);
        }
示例#19
0
 public static PropertyDescriptor GetReadOnlyPropertyProperty()
 {
     return(CustomTypeDescriptor.CreateProperty(typeof(Thing).GetProperty("ReadOnlyProperty")));
 }
示例#20
0
文件: JsonType.cs 项目: db48x/KeeFox
 private void OnChanging()
 {
     _customType = null;
 }