DataType属性通过Reflection进行装饰

Alf*_*ero 3 c# reflection

这个场景

public class CustomerMetaData
{


    [DataType(DataType.EmailAddress)]       
    public String EmailAddress {get;set;}

    [DataType(DataType.Url)]       
    public String UrlUser {get;set;}

}
Run Code Online (Sandbox Code Playgroud)

我需要通过这个类的所有属性的反射 DataType,但广泛的网络搜索,我找不到围绕这种类型的DataAttribute的任何解决方案.

我解释了一下,我不需要知道属性Like,String,Boolean ....的数据类型.我需要[DataType(DataType .....)]属性的一部分.

提前致谢.

一些想法?

Ann*_* L. 5

您需要GetCustomAttributes方法.

这是来自内存,但它会是这样的:

PropertyInfo[] props = typeof(CustomerMetaData).GetProperties();
foreach(PropertyInfo p in props)
{
    object[] attribs = p.GetCustomAttributes(false);
    // do something with the attributes
}
Run Code Online (Sandbox Code Playgroud)

查找GetProperties和GetCustomAttributes方法以确保参数:如果您的任何属性是非公共属性,则必须指定一些其他信息以获取它们的信息.