Search code examples
c#system.componentmodel

Is there a way to access an ITypeDescriptorContext or an IServiceProvider object from a custom TypeDescriptionProvider?


It seems like an odd gap in the TypeDescriptor dynamic metadata architecture. All of the TypeConverter, UITypeEditor and other dynamic designers, editors and converter classes provide access to an IServiceProvider object which allows for design context to be passed to implementing classes.

For some reason this is absolutely missing from TypeDescriptionProvider, CustomTypeDescriptor and associated interfaces. Is there any way to have access to the editor design context while dynamically extending the metadata/properties of a class?


Solution

  • After thinking more about it, I concluded that this can be solved by creating a dynamic TypeDescriptionProvider implementation which is provided by the designer itself. This implementation can receive a context directly via some domain-specific property.

    It is then possible to associate dynamic type description providers using the TypeDescriptor.AddProvider method, passing in the custom provider and the target type.