public class Customer
{
[DisplayName("姓名")]
public string Name;
}
这个Customer的Name的元数据DisplayName的值怎么得到?
如果是[Description("姓名")]呢?怎么取?
------解决方案--------------------------------------------------------
反射可以取吗?试试啊
------解决方案--------------------------------------------------------
反射
- C# code
Type type = typeof(Customer); object[] objs=type.GetCustomAttributes(true);
------解决方案--------------------------------------------------------
这个应该是定制attribute特性
一般用于附加在类或者属性方法中,标记其特性.
使用的地方用反射读出这些标记.
说起来比较长篇,去看看文章吧.
http://msdn.microsoft.com/en-us/library/aa288454.aspx
http://www.vckbase.com/document/viewdoc/?id=994
------解决方案--------------------------------------------------------
public class Customer
{
[DisplayName("姓名")]
[Descriptor("姓名备注")]
public string Name;
}
Type container = typeof(Customer);
string propertyName = "Name";
pd = TypeDescriptor.GetProperties(container).Find(propName,false);
//pd.DisplayName 姓名
//pd.Descriptor 姓名备注