Custom attribute applying another attribute

  • Thread starter Thread starter Andrew Roberts
  • Start date Start date
A

Andrew Roberts

Does anyone know is it possible to create a custom attribute, say
CustomProgIdAttribtute
That when applied to a class will also apply another attribute to it, say
ProgIdAttribute
What I want is to create a custom ProgId attribute that takes two strings and then creates the string for the ProgIdAttribute and applies it.
I thought of derivation but this won't work for several reasons especialy due to the fact that the attributes are all sealed.
Any ideas? Can you programaticaly set an attribute onto a target from the code of another that is applied to that target?
 
Andrew said:
Does anyone know is it possible to create a custom attribute, say
CustomProgIdAttribtute
That when applied to a class will also apply another attribute to it,
say
ProgIdAttribute
What I want is to create a custom ProgId attribute that takes two
strings and then creates the string for the ProgIdAttribute and
applies it. I thought of derivation but this won't work for several
reasons especialy due to the fact that the attributes are all sealed.
Any ideas? Can you programaticaly set an attribute onto a target
from the code of another that is applied to that target?

Not really.

Attributes are read by the compiler which checks for real attributes (like
DllImport and Serializable) and custom attributes (like ProgId) and then
adds them to the metadata of the type. (Real attributes will have an IL
attribute, custom attributes are serialized and applied using the .custom
directive.) If you use the Rotor C# compiler you have the source code which
you can use to interpret your attribute and add the [ProgId] attribute
accordingly, but it seems like a lot of effort.

The other thing you can do is use the classes in System.Reflection.Emit and
create your assembly. However, that is even more effort because you'll have
to generate the IL with some code.

Richard
 
Back
Top