获取对象实例的名称 [英] Get name of instance of object

查看:67
本文介绍了获取对象实例的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取对象实例的名称?


我想显示它以用于调试目的但是没有关于如何

来讨论它。


ie,我想要的东西像(假的)


A级{}


main

{

AB;


writeline(" {0}" ,B.Name());


}


并显示B.


我知道我只能简单地显示B,但如果我最终更改实例

名称,那么我必须知道在哪里更新它。我可以

重构/搜索和替换课程但最好把它放在代码中。


谢谢,

Jon

Is it possible to get the name of an instance of an object?

I want to display it for debugging purposes but don''t have a clue about how
to go about it.

i.e., I want something like(psuedo)

Class A { }

main
{
A B;

writeline("{0}", B.Name());

}

and it display B.

I know I could just simply display B but if I end up changing the instance
name then I would necessarily know where to update it then. I could
refactor/search and replace ofcourse but it seems better to put it in code.

Thanks,
Jon

推荐答案



我还需要打印出变量的地址......似乎无法做到这一点

或者当我只是输入对象名称来打印它时就好像

我使用GetType()

I also need to print out the addresses of variables... can''t seem to do this
either as when I just put in the object name to print it doe sthe same as if
I used GetType()


Jon Slaughter< Jo *********** @ Hotmail.comwrote:
Jon Slaughter <Jo***********@Hotmail.comwrote:

是否可以获取实例的名称一个物体?
Is it possible to get the name of an instance of an object?



没有这样的概念,我很害怕。

There''s no such concept, I''m afraid.


我想显示它用于调试目的,但没有关于

如何去做的线索。


ie,我想要像(假的)


A级{}


main

{

AB;


writeline(" {0}",B.Name());


}


它显示B.


我知道我只能简单地显示B但是如果我最终更改实例

名称那么我必须知道在哪里更新然后呢。
I want to display it for debugging purposes but don''t have a clue about how
to go about it.

i.e., I want something like(psuedo)

Class A { }

main
{
A B;

writeline("{0}", B.Name());

}

and it display B.

I know I could just simply display B but if I end up changing the instance
name then I would necessarily know where to update it then.



问题是,'不是对象的名称 - 它是* a *

变量的名称它具有对象的引用。可能有多个

变量引用同一个对象,或者可能没有。


如果你想给一个对象一个名字,你就是'我必须添加它作为该类的

属性。


-

Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

The thing is, that''s not the name of the object - it''s the name of *a*
variable which has a reference to the object. There may be multiple
variables referring to the same object, or there may be none.

If you want to give an object a name, you''ll have to add that as a
property of the class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too




" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP ************************ @ msnews。 microsoft.c om ...

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...

Jon Slaughter< Jo *********** @ Hotmail.comwrote:
Jon Slaughter <Jo***********@Hotmail.comwrote:

>是否可以获取对象实例的名称?
>Is it possible to get the name of an instance of an object?



没有这样的概念,我很害怕。


There''s no such concept, I''m afraid.


>我想要显示它是为了调试目的但是没有关于
如何进行它的线索。

即,我想要类似(伪装)的东西

A类{}

主要
{
AB;

写字线(" {0}",B.Name( ));

}
它显示B.

我知道我只能简单地显示B但是如果我最终改变了
实例
名称然后我必须知道在哪里更新它。
>I want to display it for debugging purposes but don''t have a clue about
how
to go about it.

i.e., I want something like(psuedo)

Class A { }

main
{
A B;

writeline("{0}", B.Name());

}

and it display B.

I know I could just simply display B but if I end up changing the
instance
name then I would necessarily know where to update it then.



事情是,这不是对象的名称 - 它是* a *

变量的名称它具有对象的引用。可能有多个

变量引用同一个对象,或者可能没有。


如果你想给一个对象一个名字,你就是'我必须添加它作为该类的

属性。


The thing is, that''s not the name of the object - it''s the name of *a*
variable which has a reference to the object. There may be multiple
variables referring to the same object, or there may be none.

If you want to give an object a name, you''ll have to add that as a
property of the class.



我不想要对象的名称。我可以用GetType或

ToString来获得它吗?我想要实例的名称(或你所说的变量)。


所以,如果我有3个实例或引用,那么我只关心那里的名字和

不是他们指向的地方。


所以在上面如果我有A B1,B2,B3;


那么我真的只想要得到B1,B2,B3。我知道这里可能存在一些问题。但是当我以B1.Name()为例时,我希望它能够

返回B1。我不介意编译器只是向左看

了。并取出该令牌,然后用一串

替换该语句。


即,X.Name()应该返回X。会很好。


这意味着我可以完全重构,它会照顾好自己的b $ b。如果我做了类似WriteLine(X)的事情;然后我将我的

变量重命名为Y然后我仍然会打印X但是如果我有上面的

之类的东西我可以重构而不用担心它。 />

这有意义吗?


谢谢,

Jon

I don''t want the name of the object. I can get that with GetType or
ToString? I want the name of the instance(or variable as you said).

So if I have 3 instances or references then I only care about there name and
not where they point.

so in the above if I have A B1, B2, B3;

then I really only want to get B1, B2, B3. I understand that there might be
some problems here. But when I do B1.Name() for example, I would want it to
return "B1". I don''t mind the if the compiler just looked to the left of
the . and took that token and then replace the statement with a string of
it.

i.e., X.Name() should return "X" would be nice.

What this means is that I can refactor completely and it will take care of
itself. if I did something like WriteLine("X"); and I later renamed my
variable to Y then I will still be printing X but if I had something like
above I could just refactor and not worry about it.

Does that make sense?

Thanks,
Jon


这篇关于获取对象实例的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆