Source text: http://www.cnblogs.com/bicker/p/3326763.html
Add attribute to class dynamic has always been the problem I want to solve, from MSDN for a long time, to stack overflow read a lot of articles, is ultimately the answer.
First there's an explanation for this.
Attributes is static metadata. assemblies, modules, types, members, parameters, and return values aren ' t first-class objects in C # (e.g., the System.Type Class is merely a reflected representation of a type). You can get a instance of a attribute for a type and a change the properties if they ' re writable but that won ' t affect the attribute as it is applied to the type.
From this point of view, it is impossible to add attribute dynamically. Haha, the title pits it.
Okay, but I still found something. System.ComponentModel.TypeDescriptor, a class that claims to add attribute to classes and objects. There are methods in TypeDescriptor: AddAttributes, you can add attribute to the class. But there is only one problem, added to the attribute, can only be obtained through the TypeDescriptor.
/* * now you need to add Simpleobject to Attribu Te */ Typedescriptor.addattributes (
typeof (TargetObject),
new SimpleAttribute (new TargetObject ())); AttributeCollection collection = typedescriptor.getattributes (typeof (TargetObject)); SimpleAttribute attr = ((simpleattribute) collection[typeof if (attr! = null
Dynamically add attribute "Go" to a class using TypeDescriptor