Tags: adding, atruntime, atruntimewhat, attributes, class, default, editor, hiis, net, property, runtime, sharp

Adding attributes at runtime

On .Net » .Net C# (C sharp)

4,503 words with 5 Comments; publish: Fri, 04 Jan 2008 18:47:00 GMT; (10060.55, « »)

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

All Comments

Leave a comment...

  • 5 Comments
    • 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/i.../04/NETMatters/

      http://msdn.microsoft.com/msdnmag/i.../05/NETMatters/

      --

      - Nicholas Paldino [.NET/C# MVP]

      - mvp.net-csharp.todaysummary.com.spam.guard.caspershouse.com

      "florin" <dfev77.net-csharp.todaysummary.com.gmail.comwrote in message

      news:1187194622.969369.301650.net-csharp.todaysummary.com.19g2000hsx.googlegro ups.com...

      Quote:
      === Original Words ===

      >

      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

      >

      #1; Fri, 04 Jan 2008 18:49:00 GMT
    • 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/li...ttw(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

      #2; Fri, 04 Jan 2008 18:50:00 GMT
    • Replace:

      it would affect reflection

      With:

      it wouldn't affect reflection

      --

      - Nicholas Paldino [.NET/C# MVP]

      - mvp.net-csharp.todaysummary.com.spam.guard.caspershouse.com

      "Marc Gravell" <marc.gravell.net-csharp.todaysummary.com.gmail.comwrote in message

      news:1187212806.239969.230090.net-csharp.todaysummary.com.19g2000hsx.googlegro ups.com...

      Quote:
      === Original Words ===

      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/li...ttw(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

      >

      #3; Fri, 04 Jan 2008 18:51:00 GMT
    • You are absolutely right... slippy fingers - honest! I hope it was

      clear from context that this is what I meant ;-(

      Marc

      #4; Fri, 04 Jan 2008 18:51:00 GMT
    • ok, thanks guys!

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

      florin

      #5; Fri, 04 Jan 2008 18:52:00 GMT