Skip to main content
 首页 » 编程设计

c#之描述一个属性

2024年02月24日20dyllove98

如何对属性进行描述并通过 system.reflection 接收它?

请您参考如下方法:

你可以使用 custom attribute :

public class FooAttribute : Attribute 
{ 
    public string Description { get; set; } 
} 
 
public class Bar 
{ 
    [Foo(Description = "Some description")] 
    public string BarProperty { get; set; } 
} 
 
public class Program 
{ 
    static void Main(string[] args) 
    { 
        var foos = (FooAttribute[])typeof(Bar) 
            .GetProperty("BarProperty") 
            .GetCustomAttributes(typeof(FooAttribute), true); 
        Console.WriteLine(foos[0].Description); 
    } 
}