Adding attributes at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • florin

    #1

    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

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Adding attributes at runtime

    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 ICustomTypeDesc riptor 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]
    - mvp@spam.guard. caspershouse.co m

    "florin" <dfev77@gmail.c omwrote in message
    news:1187194622 .969369.301650@ 19g2000hsx.goog legroups.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
    >

    Comment

    • Marc Gravell

      #3
      Re: Adding attributes at runtime

      Note that when talking about a Type (rather than an instance), then
      the 2.0 TypeDescription Provider 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

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Adding attributes at runtime

        Replace:

        it would affect reflection

        With:

        it wouldn't affect reflection


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Marc Gravell" <marc.gravell@g mail.comwrote in message
        news:1187212806 .239969.230090@ 19g2000hsx.goog legroups.com...
        Note that when talking about a Type (rather than an instance), then
        the 2.0 TypeDescription Provider 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
        >

        Comment

        • Marc Gravell

          #5
          Re: Adding attributes at runtime

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

          Marc


          Comment

          • florin

            #6
            Re: Adding attributes at runtime

            ok, thanks guys!

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

            florin

            Comment

            Working...