简体   繁体   English

当我不知道类型时,可以获取属性的DataAnnotation显示名称吗?

[英]Can I get the DataAnnotation Display Name of a property when I don't know the type?

I am going to walk through the properties of items in an ICollection and at Compile Time will not necessarily know what type the items in the ICollection will be. 我将遍历ICollection中项目的属性,在编译时不一定会知道ICollection中项目的类型。 I can get the Property name, but would like to get the DataAnnotation Display Name (if any). 我可以获取属性名称,但是想要获取DataAnnotation显示名称(如果有)。

How can I find the Display Name defined in DataAnnotations (if any) of an unknown type at run time? 如何在运行时查找在数据类型注释(如果有)中定义的显示名称?

So far I have this: 到目前为止,我有这个:

foreach (var thisSection in Report.ReportSections)
{
    reportBody.Append(thisSection.ReportSectionName + Environment.NewLine);

    if (thisSection.ReportItems != null)
    {
        var itemType = thisSection.ReportItems.GetType().GetGenericArguments().Single();

        var first = true;
        foreach (var prop in itemType.GetProperties())
        {
            if (!first) reportBody.Append(",");

            // This gives me the property name like 'FirstName'
            reportBody.Append(prop.Name); 

            try
            {
                // I'd like to get the Display Name from 
                // [Display(Name = "First Name")]
                var displayName = prop.GetCustomAttributes();
            }
            catch (Exception e)
            {

            }

            first = false;
        }
        reportBody.Append(Environment.NewLine);
    }
}

ReportSection is defined like this: ReportSection的定义如下:

public interface IReportSection
{
    string ReportSectionName { get; }

    ICollection ReportItems { get; }
}

The ICollection could contain a collection of objects like this: ICollection可以包含这样的对象的集合:

public class ProjectAffiliateViewModel
{
    public string Role { get; set; }

    [Display(Name = "First Name")]
    public string FirstName { get; set; }
}

For the Role property we would get Role , and for the FirstName property we would get First Name . 对于Role属性,我们将获得Role ;对于FirstName属性,我们将获得First Name

Like this: 像这样:

DisplayAttribute attribute = prop.GetCustomAttributes(typeof(DisplayAttribute), false)
                                 .Cast<DisplayAttribute>()
                                 .SingleOrDefault();

string displayName = (attribute != null) ? attribute.Name : prop.Name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要获取特定对象属性的值,但不知道 object 的类型 - I need to get a value of specific object's property, but don't know the type of the object 如何获取 DataAnnotation 显示名称? - How to get DataAnnotation Display Name? 如何投放清单 <Object> 列出 <Foo> 当foo是类型变量并且我不知道类型时 - How can I cast List<Object> to List<Foo> when foo is a type variable and I don't know the type 如何将对象转换为C ++ / CLI中不知道其类型参数的通用列表? - How can I cast an Object to a generic List I don't know the type parameter of in C++/CLI? 如果我不知道属性是否在接口或具体类中,如何从属性中获取属性? - How to get attributes from a property if I don't know if they are in the interface or concrete class? 如果我不知道正确的类型,如何反序列化对象? - How can I deserialize an object if I don't know the right type? 当我不想知道子类的名称时,创建一个新的子类实例 - Create a new instance of a subclass when I don't want to know the name of it 当我不知道值的类型时,枚举IDictionary的键和值 - Enumerate keys and values of IDictionary when I don't know type of values 我有一个API,但我不知道如何获取数据 - I have an API, and I don't know how to get the data 我收到异常InvalidOperationException,但我不知道为什么 - I get an Exception InvalidOperationException and I don't know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM