Adding attributes at runtime

  • Thread starter Thread starter florin
  • Start date Start date
F

florin

hi

Is there a "simple" way to add attributes to a class/property at
runtime?

What I try to do is set the default editor for a class/property at
runtime (I know I can set this very easy by decorating the class/
property code), but I would like to do this at runtime.

Is there another way to tell the PropertyGrid what editor to use for a
class/property ?

thanks,
florin
 
florin,

If you want to add attributes to a type at runtime, then you really
can't, as a type is supposed to be static throughout the lifetime of an app,
and that includes attributes.

However, the PropertyGrid uses Type Descriptors, which have a more
limited context, and will give you what you want. Basically, you want to
create a shim class which implements the ICustomTypeDescriptor interface,
and return the property descriptors, method descriptors, and all the as well
as the Attributes that are associated with it.

Check out the .NET matters column in MSDN Magazine from April and May
2005 for an article which will help you with what you are looking for:

http://msdn.microsoft.com/msdnmag/issues/05/04/NETMatters/
http://msdn.microsoft.com/msdnmag/issues/05/05/NETMatters/
 
Note that when talking about a Type (rather than an instance), then
the 2.0 TypeDescriptionProvider implementation can be more versatile.

Something else to look at: TypeDescriptor.AddAttributes()
http://msdn2.microsoft.com/en-us/library/3zz5httw(VS.80).aspx

I've never used it, but it looks promising. Note that this only
affects the *component-model* (which is what any UI work should be
using anyway) - it would affect reflection, which looks at the
definitions at the point of compilation.

Marc
 
Replace:

it would affect reflection

With:

it wouldn't affect reflection
 
You are absolutely right... slippy fingers - honest! I hope it was
clear from context that this is what I meant ;-(

Marc
 
Back
Top