【发布时间】:2011-05-17 10:49:18
【问题描述】:
我有以下构造:
class Bla
{
public string Hello()
{
return "Hello my name is " + GetMyVariableName();
}
private string GetMyVariableName()
{
return "";//do magic here to determine the Variable name at the class that owns the Property
}
}
class Fasel
{
public Bla SomeProperty { get; set; }
public Bla AnotherProperty { get; set; }
void DoSomeAction()
{
SomeProperty = new Bla();
AnotherProperty = new Bla();
string name = SomeProperty.Hello(); //this sould return "Hello my name is SomeProperty"
name = AnotherProperty.Hello(); ////this sould return "Hello my name is AnotherProperty"
}
}
这看起来很容易解决,因为我只是通过反射来确定拥有类的名称,但在我的特殊情况下,我不能这样做。我需要在 Property 中知道它在拥有它的类中的名称是什么。
【问题讨论】:
-
如果不创建自己的 CLR 版本,您将无法执行此操作。
标签: c# reflection properties