I have the following construct:
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"
}
}
This looks easy to solve as i just do the reflection stuff to determine the name in owning class but in my special case i can’t do that. I need to know in the Property what is the name of it in the class that owns it.
This is completely, utterly, impossible.
A single instance can be referenced by multiple properties; there is no connection between an object and the properties it’s referenced by. (Except in the GC, which won’t tell you about it)
In addition, JIT inlining can mean that the property won’t necessarily be involved at all.
Plus, local variable names do not exist in optimized binaries.