热得到一个对象的名称?对象、名称

2023-09-07 09:45:38 作者:25.歡囍.

可能重复:   在C#变量打印名称

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

这是具体到.NET,但对于其他平台/语言的回答可以是有益的。

This is specific to .Net, but answer for other platform/language can be helpful.

推荐答案

源$ C ​​$ C外不存在该名称 - 要做到这一点,你就必须连接到自己作为一个调试器,或挖通过PDBS。总之,这不实际的任何措施C#和大多数其他语言。

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.