Can attributes be added dynamically in C#?

Jon Turner picture Jon Turner · Sep 24, 2008 · Viewed 100.8k times · Source

Is it possible to add attributes at runtime or to change the value of an attribute at runtime?

Answer

Alex Lyman picture Alex Lyman · Sep 24, 2008

This really depends on what exactly you're trying to accomplish.

The System.ComponentModel.TypeDescriptor stuff can be used to add attributes to types, properties and object instances, and it has the limitation that you have to use it to retrieve those properties as well. If you're writing the code that consumes those attributes, and you can live within those limitations, then I'd definitely suggest it.

As far as I know, the PropertyGrid control and the visual studio design surface are the only things in the BCL that consume the TypeDescriptor stuff. In fact, that's how they do about half the things they really need to do.