3
我在C#中创建自定义属性。通过反射属性获取有关当前属性的信息
所以我有这样的事情:
[AttributeUsage(AttributeTargets.Property)]
public class Something : Attribute
{
public Something
{
Assembly a = Assembly.GetCallingAssembly();
}
}
上面的代码将会给我哪里属性正在从被称为组件的组装名称。
我想知道的是,如果有一种方法可以获取添加属性的属性的名称和类型。因此,作为一个例子,如果我有:
// This should return System.Int32 and MyString
[Something]
public int MyInt {get; set;}
// This should return me System.String, and MyString
[Something]
public string MyString {get; set;}
有什么办法在添加的属性来获取这些值,否则我将不得不诉诸于型循环槽的所有属性,看看哪些有一个属性分配给他们?
在此先感谢。
您可以将该类型添加到该属性的构造函数中,然后通过调用4.5中的新泛型方法来检索它们。 –
我期待它能够与.Net 4.0一起使用 –
@Garrry,您有快速样例吗? – Toto