C# Attribute 简单应用。
建立一个Attribute:
public class EntityMappingAttribute : Attribute { public string TableName{get;set;} public string SortOrder { get; set; } }
建一个类:
public class User { [EntityMapping(TableName = "test",SortOrder="5")] public string name { get; set; } }
得到attribute的值 :
User u = new User();
u.name = "Andy";
PropertyInfo pi = u.GetType().GetProperty("name");
var lst = pi.GetCustomAttributes(true);
foreach (var l in lst)
{
EntityMappingAttribute ema = l as EntityMappingAttribute;
richTextBox1.Text += ema.TableName + "\n";
richTextBox1.Text += ema.SortOrder + "\n";
}