Technology Sharing

C# How to get the displayName of a property in 3 ways

2024-07-08

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


insert image description here


In C#, getting the displayName of a property can be achieved in a variety of ways, including using attributes, reflection, and LINQ. Below I will show each method separately and provide specific sample code.

1. Use direct attribute access

When defining an attribute, you can use the DisplayName attribute to specify the display name of the attribute. This method is the simplest and most direct, and is suitable for situations where the display name of the attribute needs to be specified during design.

using System;
using System.ComponentModel;

public class MyModel
{
    [DisplayName("Full Name")]
    public string Name { get; set; }
}

// 使用
MyModel model = new MyModel();
string displayName = model.Name.DisplayName; // 假设DisplayName特性已经被附加到属性上

Note: In .NET Core, the DisplayName attribute may be deprecated and you may need to use DisplayAttribute instead.

2. Use the GetCustomAttribute() method to obtain through reflection

Through reflection, you can dynamically obtain custom attributes on properties, including DisplayAttribute.

using System;
using System.ComponentModel;
using System.Reflection;

public class MyModel
{
    [Display(Name = "Full Name")]
    public string Name { get; set; }
}

// 使用
MyModel model = new MyModel();
string displayName = "";

PropertyInfo propertyInfo = model.GetType().GetProperty("Name");
DisplayAttribute displayAttribute = (DisplayAttribute)propertyInfo.GetCustomAttribute(typeof(DisplayAttribute), false);

if (displayAttribute != null)
{
    displayName = displayAttribute.Name;
}

3. Using LINQ Query

If you have a list of properties and want to query for properties with a specific display name, you can use LINQ to simplify the query process.

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

public class MyModel
{
    [Display(Name = "Full Name")]
    public string Name { get; set; }

    [Display(Name = "Date of Birth")]
    public DateTime DateOfBirth { get; set; }
}

// 使用
MyModel model = new MyModel();
string displayName = "";

var attributes = from property in model.GetType().GetProperties()
                 let displayAttribute = Attribute.GetCustomAttribute(property, typeof(DisplayAttribute)) as DisplayAttribute
                 where displayAttribute != null
                 select displayAttribute;

foreach (var attribute in attributes)
{
    if (attribute.Name == "Full Name")
    {
        displayName = attribute.Name;
        break;
    }
}

Summary and comparison

1. Use direct attribute access: The simplest way is to add the DisplayName attribute to the property. This way the display name is determined when the property is defined, and no additional query is required at runtime.

2. Use the GetCustomAttribute() method to obtain through reflection: Get the DisplayAttribute attribute on the attribute through reflection. This method dynamically obtains attribute information at runtime, which is more flexible, but the performance cost is slightly higher than directly accessing the attribute.

3. Use LINQ query: Use LINQ to query the property list and find the property with a specific display name. This method is suitable for filtering when there are a large number of properties, but it may be too complicated and is not the best choice for simple scenarios.

Each method has its applicable scenarios. In actual development, the most appropriate method should be selected based on specific needs and performance considerations. If there are few attributes and the display name is known when defining, using attributes is the simplest and most direct method. If you need to dynamically obtain attribute information or there are many attributes, using reflection or LINQ may be more appropriate.