【发布时间】:2011-01-20 17:54:32
【问题描述】:
假设我有一个名为 Test 的类,它有一个名为 Title 的属性和一个自定义属性:
public class Test
{
[DatabaseField("title")]
public string Title { get; set; }
}
还有一个名为 DbField 的扩展方法。我想知道在 c# 中是否可以从对象实例中获取自定义属性。
Test t = new Test();
string fieldName = t.Title.DbField();
//fieldName will equal "title", the same name passed into the attribute above
这个可以吗?
【问题讨论】:
-
这可能晚了,但请查看采用实例的 TypeDescriptor 和 GetAttributes 方法。这是一个非常好的帖子,用法相同:geekswithblogs.net/abhijeetp/archive/2009/01/10/…
标签: c# .net reflection custom-attributes