如何使用 C# 属性?
我必须从几个类创建一个 pdf 报告,其中将包含多个属性。 我需要显示属性的值及其前面的标签。
像这样的东西:
详细计算:
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());
}
}
}
我想知道在访问属性时是否可以访问我的属性的值?
感谢您的帮助,
纪尧姆
I have to create a pdf report from several classes that are going to contains several properties.
I need to display the value of the propertie and a label in front of it.
Something like :
detailsCalcul :
Numero client : valueOfMyProperty.
...
I was thinking of doing something like this :
[NomRapport("detailsCalcul")]
public class MyClass
{
[NomChamp("Numero client")]
public string NumeroClient { get; set; }
}
I sucessfully acessed to the value of my two attributes :
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());
}
}
}
i would like to know if it is possible access to the value of my property while I am acessing to the attribute ?
Thanks for your help,
Guillaume
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
属性不知道它所应用的上下文;你甚至无法到达该财产,更不用说实例了。但是,如果您有一个 PropertyInfo 和一个实例,那么:
An attribute does not know the context to which it is applied; you cannot even get to the property, let alone the instance. However, if you have a PropertyInfo and an instance, then: