<分区>
考虑到 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))]
我现在明白了。谢谢。