我可以在 C# 中获取对象的名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12241136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:35:48  来源:igfitidea点击:

Can I get the name of my object in C#?

c#

提问by rock

Let us say I have the following class..

假设我有以下课程..

Class Boy
{
    public void hello()
    {
        Console.WriteLine("Hello!");
    }
    static void Main(String[] args)
    {
            Boy a = new Boy();
            a.hello();
    }
}

I know that the variable 'a'is a reference variable of type 'Boy'. The keyword 'new'created an object of 'Boy'assigning the address of it to the variable 'a'.

我知道变量'a''Boy'类型的引用变量。关键字'new'创建了一个'Boy'的对象,并将其地址分配给变量'a'

Now, is it possible for me to get the name of my object. Does an object in C# have a name at all ?

现在,我是否有可能获得我的对象的名称。C# 中的对象到底有没有名字?

采纳答案by Marc Gravell

I'm guessing you are referring to the name of the variable, "a". Well, the thing is: that isn'tthe name of the object - objects don't have names. Further, an object can have zero one or multiple references to it, for example:

我猜你指的是变量的名称,“a”。好吧,问题是:那不是对象的名称——对象没有名称。此外,一个对象可以有零个或多个引用,例如:

var x = new SomeType(); // a class
var y = x;

Here both x and y refer to the same object. Equally:

这里 x 和 y 指的是同一个对象。一样:

new SomeType();

doesn't have any references to it, but is still an object. Additionally, method variables (locals) don't actually have names in IL - only in c# (for contrast, fields do have names). So new SomeType().WriteName();would make no sense.

没有对它的任何引用,但仍然是一个对象。此外,方法变量(局部变量)在 IL 中实际上没有名称 - 仅在 c# 中(相比之下,字段确实有名称)。所以new SomeType().WriteName();就没意思了。

So no: there is no sane way of getting the name of the variable from an object reference.

所以不:没有从对象引用中获取变量名称的理智方法。

If you want the object to have a name, add a Nameproperty.

如果您希望对象具有名称,请添加Name属性。

There are some fun ways to get the name of a variable of field using expression trees, but I don't think that is useful to what you are trying to do.

有一些有趣的方法可以使用表达式树获取字段变量的名称,但我认为这对您尝试做的事情没有用。

回答by adatapost

We don't have a name of objects. In fact object's reference is assigned to the referencevariable in order to access datavia public methods/properties. If you wish to have the reference of an object within the instancemethod of that particular class then use thisvariable.

我们没有对象的名称。事实上,对象的引用被分配给引用变量,以便通过公共方法/属性访问数据。如果您希望在该instance特定类的方法中拥有对象的引用,请使用this变量。

Class Boy
{   
    int age;
    public void setAge(int age)
    {
        this.age = age; 
    }
}

回答by Navin Kumar

class Boy
{
    public void hello()
    {
        Console.WriteLine("Hello!");
    }
    static void Main(String[] args)
    {
        Boy a = new Boy();
        a.hello();
        Type objtype = a.GetType();
        Console.WriteLine(objtype.Name); // this will display "Boy"
    }
}