467,146 Members | 1,060 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Adding attributes at runtime


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

Aug 15 '07 #1
  • viewed: 3318
Share:
5 Replies
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/is...04/NETMatters/
http://msdn.microsoft.com/msdnmag/is...05/NETMatters/
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"florin" <df****@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>
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

Aug 15 '07 #2
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/lib...tw(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

Aug 15 '07 #3
Replace:

it would affect reflection

With:

it wouldn't affect reflection
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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/lib...tw(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

Aug 15 '07 #4
You are absolutely right... slippy fingers - honest! I hope it was
clear from context that this is what I meant ;-(

Marc
Aug 16 '07 #5
ok, thanks guys!

at least right now I have a starting point, an not 5 :)

florin

Aug 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Maersa | last post: by
9 posts views Thread by James Geurts | last post: by
1 post views Thread by Rob | last post: by
3 posts views Thread by Robert Bull | last post: by
5 posts views Thread by Steven Berkovitz | last post: by
2 posts views Thread by prabhupr@hotmail.com | last post: by
3 posts views Thread by astruyk@gmail.com | last post: by
3 posts views Thread by zslevi@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.