I. Theory
Attribute is translated into Attribute in msdn, which is not suitable for me. I prefer the title of the predecessor Wang Tao (author of <You must know. net>)-feature.
After reading the books of Wang Tao's predecessors, I remember that Wang Tao's predecessors described the features as follows: feature attribute is essentially a class that provides associated additional information for the target element,
In the runtime, the system obtains additional information through reflection. This is not comprehensive. However, there is not much time to find relevant information.
Ii. Example
0. Example scheme diagram
1. custom Attribute (DefaultAttribute. cs)
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace TestAttribute
{
Public class DefaultAttribute: Attribute
{
Public string DefaultName {get; set ;}
}
}
2. Use custom Attribute (Model. cs)
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace TestAttribute
{
Public class Model
{
[Default (DefaultName = "name")]
Public String name {get; set ;}
[Default (DefaultName = "")]
Public Byte Score {get; set ;}
}
}
3. Test button code (as shown below)
String modelpropertiesattributes = "";
Type t = typeof (Model );
PropertyInfo [] pis = t. GetProperties ();
Foreach (PropertyInfo pi in pis)
{
Modelpropertiesattributes + = (DefaultAttribute) pi. GetCustomAttributes (true) [0]). DefaultName + ":";
}
Modelpropertiesattributes = modelpropertiesattributes. Substring (0, modelpropertiesattributes. Length-1 );
MessageBox. Show (modelpropertiesattributes );
4. Test results (e.g)