繁体   English   中英

如何使用c#属性?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM