이론적으로는 불가능하다고 생각합니다. 이 시나리오에 대한 생각 :
MyClass a, b;
a = new MyClass();
b = a;
Console.WriteLine("name of b is " + SomeMagicClass.GetVarName(b));
//Should it be "b" or "a"?
심지어 런타임에 존재하지 변수 이름의 라인을 따라 생성에 관한 코드를 포함하는 더 나은 설명이 확신합니다.
편집 아아, 틀렸어. Jon Skeet의 Null 참조 예외 처리에 대한 post에서 영감을 받아 갑자기 프로젝션에 대해 상기시켜주는 방법이 있습니다.
여기에 전체 작업 codez입니다 : 자체 메모리에 있고 이름이없는
목적 : 다음과 같은 이유로 이해가되지 않습니다
public static class ObjectExtensions {
public static string GetVariableName<T>(this T obj) {
System.Reflection.PropertyInfo[] objGetTypeGetProperties = obj.GetType().GetProperties();
if(objGetTypeGetProperties.Length == 1)
return objGetTypeGetProperties[0].Name;
else
throw new ArgumentException("object must contain one property");
}
}
class Program {
static void Main(string[] args) {
string strName = "sdsd";
Console.WriteLine(new {strName}.GetVariableName());
int intName = 2343;
Console.WriteLine(new { intName }.GetVariableName());
}
}
복제를 http://stackoverflow.com/questions/729803/print-name-of-the-variable-in-c의 –
객체를 다음과 같이
당신은 그것을 사용할 수 있습니다 이름이 없어. 변수는 이름을 가지며 변수는 객체에 _references_를 보유합니다. 또한 두 개의 변수 (이름이 다를 수 있음)가 동일한 객체를 참조 할 수 있습니다. –
구체적으로 어떤 언어를 사용합니까? 나는 VB.NET을 만들려고 노력했다. 아무런 운이 없다. – Moshe