Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is ICustomTypeDescriptor and when to use it?

I don't understand what it is and when to use it. MSDN didn't help me. MSDN states that ICustomTypeDescriptor Provides an interface that supplies dynamic custom type information for an object.

like image 309
Attilah Avatar asked Oct 03 '11 21:10

Attilah


2 Answers

There are a lot of resources available that show different use cases for the ICustomTypeDescriptor interface, but in short, the typical use case is to provide custom type description outside of what the standard TypeDescriptor provides. The interface is rarely implemented without needing to return custom member descriptors such as a custom PropertyDescriptor.

The Type Descriptor Overview from the MSDN is a good resource which might help further clarify purpose and usage.

like image 126
sellmeadog Avatar answered Nov 17 '22 08:11

sellmeadog


In MVVM you are supposed to create your ViewModels as plain C# classes exposing properties that can be bound to controls and possibly implementing INotifyPropertyChanged for issuing notifications when you change values of your properties so that the bound controls can take notice. WPF will use reflection to discover the properties of your ViewModel classes.

However, it is conceivable that you might not want WPF to use reflection for discovering properties on your objects. It is conceivable that your ViewModel might not even implement C# properties and instead it might expose named values using some other mechanism. For example, you might build a general-purpose ViewModel which dcontains an IDictionary<string,object> populated with named values. If ICustomTypeDescriptor did not exist, you would not be able to do this. ICustomTypeDescriptor tells WPF to refrain from using reflection to discover the properties of your ViewModel, and instead to discover them by invoking methods of your ICustomTypeDescriptor interface.

Further reading can be found here: https://learn.microsoft.com/en-us/archive/msdn-magazine/2005/april/net-matters-icustomtypedescriptor-part-1

like image 1
Mike Nakis Avatar answered Nov 17 '22 07:11

Mike Nakis