添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Consider the following code.

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

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() 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.

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.

The TypeDescriptor class is used in designers, so that they can interact with the design-time environment. In particular, designers can override and extend various features of TypeDescriptor, but not Type.

One good example is working with data-bound controls. The DataSource property is of type System.Object, yet at design-time, that property is replaced with a new property that has a richer design-time UI.

It's used in designers, but it's also used for data binding, so it isn't just a design-time technology. – Adam Robinson Sep 9, 2009 at 21:53

The TypeDescriptor class returns PropertyDescriptor objects that represent the properties in the type of obj as well as any other properties that were contributed to the object itself or to its type.

The component model is actually quite complex, but as you describe, the basic implementation of TypeDescriptor.GetProperties() will return ReflectPropertyDescriptor instances that represent an indirection to typical PropertyInfo objects. You can use PropertyDescriptor objects very much like PropertyInfo objects: they can be used to get and set the value of the property and they have attributes.

For DependencyObject instances in WPF, TypeDescriptor.GetProperties() also returns attached properties. These PropertyDescriptor objects in this case provide an indirection to the dependency object model rather than to reflection.

In the component designer, it is possible to use ICustomTypeDescriptor or TypeDescriptionProviderAttribute (and maybe some other techniques) to create your own PropertyDescriptor objects at runtime or at designtime. In any case, it is possible that the properties returned from Type.GetProperties() may very well differ from those returned from TypeDescriptor, depending on the context.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.