views:

511

answers:

1

I am trying to add a property to a type using TypeDescriptor.CreateProperty in order to display an additional property in a property grid, however this new property is not added and when I call TypeDescriptor.GetProperties on that type to inspect the properties, this property does not exist.

It there something I might be missing or overlooking? This is a basic and simple scenario as far as I remember.

Here is the call: TypeDescriptor.CreateProperty(typeof (MovieMenuItem), "ExternalMediaLocation", typeof (string), null);

+3  A: 

CreateProperty just gives you back a reflection-based PropertyDescriptor for the type in question (it isn't AddProperty, for example). What is the scenario here? If you just need to display extra data in DataGridView, the simplest option is simply to add an extra unbound column to the grid.

You can extend types at runtime, but for lists you have two main options:

  • ITypedList (if each instance of the list can have different columns) - see this answer
  • TypeDescriptionProvider - allows you to add custom properties per-type (ultimately boils down to writing a PropertyDescriptor, just like the first example - but different hooks)

The full rules of how list-based metadata is fetched are in this answer

Marc Gravell
I want to add it to an object that I edit using a PropertyGrid control
reshefm
Long time since the last time I "heavily" used component model... the type description provider is the missing link. Thanks!
reshefm