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

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

问题描述

考虑以下代码.

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

我正在尝试了解 A 和 B 之间的区别.据我了解 TypeDescriptor.GetProperties() 将返回自定义 TypeDescriptor 属性,其中 Type.GetProperties() 只会返回内在的真实"对象的属性.这是正确的吗?如果 obj 没有任何自定义 TypeDescriptor 属性,那么它只是默认返回对象的文字内在属性.

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.

推荐答案

obj.GetType().GetProperties() 不返回 PropertyDescriptorCollection,它返回一个 <代码>System.Reflection.PropertyInfo[].正如您所建议的, PropertyInfo 类确实仅表示在对象上创建的实际属性.PropertyDescriptor 要么是 PropertyDescriptor 类的自定义具体子类(由定义自定义描述符的类型实现),要么是密封内部类 ReflectPropertyDescriptor 的实例 使用 PropertyInfo 类来提供属性的动态调用.

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 the sealed internal class ReflectPropertyDescriptor that uses the PropertyInfo class to provide dynamic invocation of the property.

因此,对于未定义自定义描述符的类,您将在功能上取回相同的对象,尽管 PropertyDescriptor 抽象掉了 PropertyInfo.

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天全站免登陆