【发布时间】:2017-10-06 15:18:56
【问题描述】:
考虑到Myatt 属性和MyObj 类,奇怪的是ObjName 属性在Myatt 属性的范围内是已知的。不是吗?
[AttributeUsage(AttributeTargets.Property)]
public class MyAtt : Attribute
{
public MyAtt(string name)
{
this.Name = name;
}
public string Name
{
get; set;
}
}
public class MyObj
{
[MyAtt(nameof(ObjName))] //Can access to ObjName?!
public int ObjID
{
get;
set;
}
public string ObjName
{
get;
set;
}
}
更新:
对不起,我想知道为什么第一种情况是不可能的,而第二种情况是可能的。
1. [MyAtt(nameof(this.ObjName))]
2. [MyAtt(nameof(ObjName))]
我现在明白了。谢谢。
【问题讨论】:
-
你有什么问题?如果我们也觉得您的代码有效吗?
标签: c# nameof .net-attributes