C# 类的属性想添加Caption属性
在C#中,可以使用自定义属性(Custom Attribute)来添加Caption属性,如下所示:
[AttributeUsage(AttributeTargets.Property)]
public class CaptionAttribute : Attribute
{
private string caption;
public CaptionAttribute(string caption)
{
this.caption = caption;
}
public string Caption
{
get { return this.caption; }
set { this.caption = value; }
}
}
public class MyClass
{
[Caption("My Property")]
public int MyProperty { get; set; }
}
在上面的代码中,我们定义了一个CaptionAttribute类,它继承自Attribute类。在MyClass类中,我们使用Caption属性来标记MyProperty属性的标题。这样,我们就可以通过反射获取MyProperty属性的标题了。
原文地址: https://www.cveoy.top/t/topic/bqcx 著作权归作者所有。请勿转载和采集!