TypeDescriptor.GetProperties()与Type.GetProperties() [英] TypeDescriptor.GetProperties() vs Type.GetProperties()

查看:773
本文介绍了TypeDescriptor.GetProperties()与Type.GetProperties()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code。

  obj对象;
PropertyDescriptorCollection A = TypeDescriptor.GetProperties(OBJ);
的PropertyInfo [] B = obj.GetType()的GetProperties(); //编辑*

我想知道从我的理解A和B之间的区别的 TypeDescriptor.GetProperties()返回定制的 TypeDescriptor 属性,其中为 Type.GetProperties()只会返回对象的内在真实的特性。这是正确的吗?如果obj没有任何自定义的 TypeDescriptor 属性,然后它只是默认也返回对象的字面内在属性。


*原 code的第二行编辑之前(有错误的返回值):


  

<击> PropertyDescriptorCollection B = obj.GetType()的GetProperties();



解决方案

obj.GetType()的GetProperties()不返回 PropertyDescriptorCollection ,它返回一个 System.Reflection.PropertyInfo [] 。在的PropertyInfo 类呢,正如你提到的,在对象上重新创建present只有实际属性。 A 的PropertyDescriptor 或者是的PropertyDescriptor 类的自定义具体子(定义自定义描述的类型实现的),或是的一个实例 ReflectPropertyDescriptor 使用了的PropertyInfo 类提供的财产动态调用。

因此​​,对于没有自定义一个描述符类,你会在功能上得到相同的对象回来,虽然的PropertyDescriptor 被抽象掉了的PropertyInfo

Consider the following code.

Object obj;
PropertyDescriptorCollection A = TypeDescriptor.GetProperties(obj);
PropertyInfo[] B = obj.GetType().GetProperties(); // EDIT*

I'm trying to understand the difference between A and B. From what I understand TypeDescriptor.GetProperties() will return custom TypeDescriptor properties, where as Type.GetProperties() will only return intrinsic "real" properties of the object. Is this right? If obj doesn't have any custom TypeDescriptor properties then it just defaults to also returning the literal intrinsic properties of the object.


* Original second line of code before EDIT (had wrong return value):

PropertyDescriptorCollection B = obj.GetType().GetProperties();

解决方案

obj.GetType().GetProperties() does not return a PropertyDescriptorCollection, it returns a System.Reflection.PropertyInfo[]. The PropertyInfo class does, as you suggest, represent only actual properties created on the object. A PropertyDescriptor is either a custom concrete child of the PropertyDescriptor class (implemented by the type defining the custom descriptor), or is an instance of ReflectPropertyDescriptor that uses the PropertyInfo class to provide dynamic invocation of the property.

So for a class that does not define a custom descriptor, you will functionally get the same objects back, though the PropertyDescriptor is abstracting away the PropertyInfo.

这篇关于TypeDescriptor.GetProperties()与Type.GetProperties()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆