[英]How can I use c# attributes?
我必須從幾個包含多個屬性的類創建一個pdf報告。 我需要在其前面顯示屬性的值和標簽。
就像是 :
detailsCalcul:
Numero客戶端:valueOfMyProperty。
...
我正在考慮做這樣的事情:
[NomRapport("detailsCalcul")]
public class MyClass
{
[NomChamp("Numero client")]
public string NumeroClient { get; set; }
}
我成功地獲得了我的兩個屬性的值:
System.Reflection.MemberInfo[] proprietes = typeof(MyClass).GetMembers();
MyClass client = new MyClass();
client.NumeroClient = "1234";
foreach (var p in proprietes)
{
var aa = p.GetCustomAttributes(true);
for (int i = 0; i < aa.Length; i++)
{
var test = aa[i];
if (test.GetType() == typeof(NomChampAttribute))
{
var nomChamp = ((NomChampAttribute)attributes[i]).ToString());
}
}
}
我想知道在訪問屬性時是否可以訪問我的屬性的值?
謝謝你的幫助,
紀堯姆
屬性不知道對其應用的上下文。 您甚至無法訪問該屬性,更不用說實例了。 但是,如果您有一個PropertyInfo和一個實例,則:
object value = property.GetValue(instance, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.