小编kab*_*cey的帖子

如何从C#中的对象实例获取自定义属性

假设我有一个名为Test的类,其中一个名为Title的属性带有自定义属性:

public class Test
{
    [DatabaseField("title")]
    public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有一个名为DbField的扩展方法.我想知道是否可以在c#中从对象实例获取自定义属性.

Test t = new Test();
string fieldName = t.Title.DbField();
//fieldName will equal "title", the same name passed into the attribute above
Run Code Online (Sandbox Code Playgroud)

可以这样做吗?

.net c# reflection custom-attributes

23
推荐指数
2
解决办法
4万
查看次数

标签 统计

.net ×1

c# ×1

custom-attributes ×1

reflection ×1