0
我有一个称为“InfoTest”的“Form1”类的属性,它具有一些我想要访问的自定义属性。访问自定义属性的更简单方法?
的代码工作正常,但它是一个有点笨拙:
[Test("foo",15)]
public double InfoTest { get; set; }
public void RetrieveAttribute()
{
PropertyInfo field_info = typeof(Form1).GetProperty("InfoTest");
object[] custom_attributes = field_info.GetCustomAttributes(typeof(TestAttribute), false);
TestAttribute thisAttribute = (TestAttribute)custom_attributes[0];
Debug.WriteLine(thisAttribute.Info + "," + thisAttribute.TheValue);
}
我相信答案是“否”,但有可能得到InfoTest属性的一个更简单的方法,即不涉及`typeof运算(Form1上).GetProperty( “InfoTest”)?我不能去(例如):
var runtimePropInfo = InfoTest.GetType().GetRuntimeProperties();
var propInfo = InfoTest.GetType().GetProperties();
...这基本上是因为它试图得到一个“双”,而不是“InfoTest”对象的属性。
我不认为有什么简单的办法,[因为这链接](https://stackoverflow.com/a/6637710/6741868)也显示,该解决方案你目前使用的似乎已经是最佳的了。 –
你可以通过使用'TestAttribute thisAttribute = field_info.GetCustomAttribute(false);'来简化它。这会删除'object'数组和cast,但您仍然需要'PropertyInfo'(实际上它的基类'MemberInfo')作为扩展方法。 –