通过反射获取类实例名称 [英] Getting class instance names via reflection
问题描述
我想获得一个
''对象'的类INSTANCE名称(不是类型名称)。
我可以得到对象(l_obj_ref.GetType())然后获取类名的
(l_obj_typ.Name)。
我有办法获得''l_Fred_my_ins ''出于以下情况。
....
My_cls l_Fred_my_ins = new My_cls();
输入l_rfl_trg_typ = l_my_ins.GetType();
string l_ins_nam_str = l_rfl_trg_typ.GetClassInstanceName();
....
我想''l_ins_nam_str''实例名称用于调试目的(现在为
)。
所以我要打印出来实例名称(模式实例化)。
非常感谢您的回复。
Shawnk
PS。我正在寻找
''l_whatever.GetClassInstanceName''的句法推论。
I would like to get the class INSTANCE name (not type name) of an
''object''.
I can get the object (l_obj_ref.GetType()) and then get the
(l_obj_typ.Name) for the class name.
I there any way of getting ''l_Fred_my_ins'' out of the following.
....
My_cls l_Fred_my_ins = new My_cls();
Type l_rfl_trg_typ = l_my_ins.GetType();
string l_ins_nam_str = l_rfl_trg_typ.GetClassInstanceName();
....
I would like to ''l_ins_nam_str'' instance names for debugging purposes (for
now).
So I want to print out the instance names (of pattern instantiations).
Thanks much for any response.
Shawnk
PS. I''m looking for the syntactic corollary to
''l_whatever.GetClassInstanceName''.
推荐答案
不确定你是否看过这个帖子......或者它会有所帮助..仔细阅读它...,我现在已经出去了... b $ b当我回来时会尽力帮助..
http://groups.google.com/group/comp....cc2ccf047d2063
VJ
" Shawnk" <嘘**** @ discussions.microsoft.com>在消息中写道
news:64 ********************************** @ microsof t.com ...
Not sure if you saw this thread... or it will help.. read through it.. , I
am out now...will try to help when I get back..
http://groups.google.com/group/comp....cc2ccf047d2063
VJ
"Shawnk" <Sh****@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com...
我想获得一个
''对象'的类INSTANCE名称(不是类型名称)。
我可以获取对象(l_obj_ref.GetType()),然后获取类名的
(l_obj_typ.Name)。
我有任何办法让''l_Fred_my_ins''脱离以下。
...
My_cls l_Fred_my_ins = new My_cls();
输入l_rfl_trg_typ = l_my_ins.GetType();
string l_ins_nam_str = l_rfl_trg_typ.GetClassInstanceName();
...
我想''l_ins_nam_str''实例名称用于调试目的(现在为
)。
所以我想打印出实例名称(模式实例化)。
非常感谢您的回复。
Shawnk
> PS。我正在寻找
''l_whatever.GetClassInstanceName''的句法推论。
I would like to get the class INSTANCE name (not type name) of an
''object''.
I can get the object (l_obj_ref.GetType()) and then get the
(l_obj_typ.Name) for the class name.
I there any way of getting ''l_Fred_my_ins'' out of the following.
...
My_cls l_Fred_my_ins = new My_cls();
Type l_rfl_trg_typ = l_my_ins.GetType();
string l_ins_nam_str = l_rfl_trg_typ.GetClassInstanceName();
...
I would like to ''l_ins_nam_str'' instance names for debugging purposes (for
now).
So I want to print out the instance names (of pattern instantiations).
Thanks much for any response.
Shawnk
PS. I''m looking for the syntactic corollary to
''l_whatever.GetClassInstanceName''.
>我有办法得到' 'l_Fred_my_ins''出于以下情况。
如果它是某种类型的字段,您可以使用该类型的反射来查找字段
名称。当然很多字段可能指的是同一个对象所以
你不能保证找到正确的匹配。
如果它是一个局部变量这是不可能的。编译期间局部变量名称为
,仅在调试符号中可用。
Mattias
-
Mattias Sj?gren [C#MVP] mattias @ mvps.org
http ://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请回复到新闻组。
>I there any way of getting ''l_Fred_my_ins'' out of the following.
If it''s a field in a type, you can use reflection on that type to find
the field name. Of course many fields may refer to the same object so
you''re not guaranteed to find a correct match.
If it''s a local variable it''s not possible. Local variable names are
lost during compilation and only available in debug symbols.
Mattias
--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Shawnk< Sh **** @ discussion.microsoft.com>写道:
Shawnk <Sh****@discussions.microsoft.com> wrote:
我想获得一个
''对象'的类INSTANCE名称(不是类型名称)。
I would like to get the class INSTANCE name (not type name) of an
''object''.
没有这样的事情。例如:
对象x =新对象();
对象y = x;
有'只涉及一个对象 - 它的名字是什么?
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet
如果回复该组,请不要给我发邮件
There''s no such thing. For instance:
object x = new object();
object y = x;
There''s only one object involved - what''s its name?
--
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
这篇关于通过反射获取类实例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!