Possible Duplicate:
print name of the variable in c#
How I can print the name of any object
MyClass c1, c2;
printName(c1);
printName(c2);
void printName(Object o)
{
Console.WriteLine("name of object : "+ o开发者_如何学编程.???());
}
output should be like this:
name of object : c1
name of object : c2
This is specific to .Net, but answer for other platform/language can be helpful.
The name doesn't exist outside of the source code - to do this, you would have to be attached to yourself as a debugger, or dig through the PDBs. In short, this not practical in any measure for C# and most other languages.
That does not make sense for the following reason:
The object itself is in memory and has no name. You are accessing it using a reference which has a name. Thus, the reference name can change in any moment, you can have 50 references "pointing" to the same nameless object, etc.
Consider this:
MyClass c1, c2;
c1 = new MyClass();
c2 = c1;
printName(c1);
printName(c2);
As you can see, both c1 and c2 are references to the exact same object which has no way of "knowing" who is referencing it or by which name.
This is impossible.
What is the result of this?
string s = "Hello, world!";
string t = s;
printName(t);
As s
and t
both refer to the same instance of string
there is no way to distinguish between invocations of printName
with s
as the parameter versus t
as the parameter.
And what should be the result of this?
printName("Hello, world!");
I don't think it's theoretically possible. Think about this scenario:
MyClass a, b;
a = new MyClass();
b = a;
Console.WriteLine("name of b is " + SomeMagicClass.GetVarName(b));
//Should it be "b" or "a"?
I am sure there is a better explanation involving generated MIDL code along the lines of variable name not even being present at runtime.
EDIT Alas I was wrong. Inspired by Jon Skeet's post about Null Reference exception handling and suddenly being reminded about projection there is a way to kinda do that.
Here is complete working 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());
}
}
That is not possible.
Name of the variable is of importance only to the developer (not the compiler or runtime).
You could create a Dictionary<string, object>
& add those instances with the name of their variable to achieve something like that.
EDIT: And that is the reason, it is said - write code for people to understand and incidentally for the compiler.
You need to place a Name property inside your MyClass class, eg.
class MyClass {
public string Name {get;set;}
// rest of class goes here
// ...
}
then you can use it as follows:
var c1 = new MyClass() { Name = "c1" };
var c2 = new MyClass() { Name = "c2" };
printName(c1);
printName(c2);
void printName(MyClass o)
{
Console.WriteLine("name of object : "+ o.Name);
}
精彩评论