在 .net 中,我如何获取声明类型中的对象名称。例如……
public static void Main()
{
Information dataInformation = new Information();
}
public class Inforamtion
{
//Constructor
public Inforamtion()
{
//Can I fetch name of object i.e. "dataInformation" declared in Main function
//I want to set the object's Name property = dataInformation here, because it is the name used in declaring that object.
}
public string Name = {get; set;}
}
最佳答案
就 CLR 而言,并没有真正确定对象名称的方法。这类信息(在某种程度上)存储在调试信息和程序集中,但不会在运行时使用。无论如何,您所指的对象只是内存中的一堆字节。它可以通过多个名称对它进行多次引用,因此即使您可以获得引用该对象的所有变量的名称,也无法以编程方式确定您要使用哪个变量。
长话短说:你不能那样做。
关于c# - 如何在 .net 中使用反射获取对象名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1653450/