Object.GetType - 如何正确调用 [英] Object.GetType - how to call correctly

查看:63
本文介绍了Object.GetType - 如何正确调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




原来可以用Object.GetType

方法欺骗另一种类型。如果你这样做:


命名空间N {

class C {

public new Type GetType(){

return(String.Empty.GetType());

}

}

}


然后在你的代码中的某个地方:


C c = new C();

Console.WriteLine(c.GetType() )


输出将是System.String


显然,使其正常工作的方法是向上转换

对象:


C c = new C();

Console.WriteLine(((Object)c).GetType( ))


将会产生N.C

我将非常感谢您对此的评论。


Yan

解决方案

严。

确实可以使用&覆盖GetType方法;新"运算符,

但考虑一个真实世界的用例:如果你知道你有一个类型C的引用

,你就没有必要调用GetType获取

a引用类型C。相反,您将使用typeof运算符,即在编译时解析的
。只有当你无法弄清楚编译时的类型

类型,例如:有一个对象引用,你会调用GetType来获得它的

类型。所以,我不担心。

如果你想保持安全,当然,转换它更安全

到对象在调用GetType之前。


问候

陈明


" Yan Vinogradov" <雅************ @ Marconi.com>在消息中写道

新闻:56 ************************* @ posting.google.co m ... < blockquote class =post_quotes>

原来可以用Object.GetType
方法欺骗另一种类型。如果你这样做:

命名空间N {
C类{
public new Type GetType(){
return(String.Empty.GetType());
}
}
}

然后在你的代码中的某个地方:

C c = new C();
Console.WriteLine(c.GetType())

输出将是System.String

显然,使其正常工作的方法是向上转换参考
对象:

C c = new C();
Console.WriteLine(((Object)c).GetType())

将会是N.C

我将非常感谢您对此的评论。

Yan





我看到没有什么奇怪的,GetType是一种方法,就像任何

其他方法一样,你可以给它一个新的意思,并以这种方式你可以垃圾

方法的初衷,甚至给出一个完全不同的

意思,来自谁写的坏代码,'' s al l。


为什么你看到它有什么奇怪的感觉?


欢呼,


- -

Ignacio Machin,

ignacio.machin at dot.state.fl.us

佛罗里达州交通局

Yan Vinogradov <雅************ @ Marconi.com>在消息中写道

新闻:56 ************************* @ posting.google.co m ... < blockquote class =post_quotes>

原来可以用Object.GetType
方法欺骗另一种类型。如果你这样做:

命名空间N {
C类{
public new Type GetType(){
return(String.Empty.GetType());
}
}
}

然后在你的代码中的某个地方:

C c = new C();
Console.WriteLine(c.GetType())

输出将是System.String

显然,使其正常工作的方法是向上转换参考
对象:

C c = new C();
Console.WriteLine(((Object)c).GetType())

将会是N.C

我将非常感谢您对此的评论。

Yan


你好明陈,


我对C#的经验很少,但是你不可能获得对b / b
的参考一个C对象,它有许多子类,

,这个引用可以指向任何这些

子类的实例,你可以调用GetType()来知道AC tual类型,但获得

一些欺骗的价值作为回报?


谢谢,

Yan

Hi,

Turns out it''s possible to spoof another type with Object.GetType
method. If you do this:

namespace N {
class C {
public new Type GetType() {
return (String.Empty.GetType());
}
}
}

and then somewhere in the code you do:

C c = new C();
Console.WriteLine(c.GetType())

the output will be "System.String"

Apparently the way to make it work right is to upcast the reference to
Object:

C c = new C();
Console.WriteLine(((Object)c).GetType())

will yeild "N.C"
I''d appreciate your comments on that.

Yan

解决方案

Hi, Yan.
It''s true that you can overwrite the GetType method with "new" operator,
but consider a real world use case: If you know that you have a reference to
type C, it wouldn''t be necessary for you to call GetType on top of it to get
a reference to Type C. You would, instead, use typeof operator, which is
resolved at compile time. Only when you can''t figure out the type at compile
type, E.g: having an object reference, would you call GetType to get its
type. So, I wouldn''t worry about it.
If you want to stay at the safe side, of course, it''s safer to convert it
to Object before calling GetType.

Regards
Ming Chen

"Yan Vinogradov" <Ya************@Marconi.com> wrote in message
news:56*************************@posting.google.co m...

Hi,

Turns out it''s possible to spoof another type with Object.GetType
method. If you do this:

namespace N {
class C {
public new Type GetType() {
return (String.Empty.GetType());
}
}
}

and then somewhere in the code you do:

C c = new C();
Console.WriteLine(c.GetType())

the output will be "System.String"

Apparently the way to make it work right is to upcast the reference to
Object:

C c = new C();
Console.WriteLine(((Object)c).GetType())

will yeild "N.C"
I''d appreciate your comments on that.

Yan



Hi,

I see nothing weird there, GetType is a method in the same way that any
other method, you can give it a new meaning , and in such way you can trash
the original intention of the method and even give a complete different
meaning, bad code from who write it, that''s all.

Why you see it weird in any way?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Yan Vinogradov" <Ya************@Marconi.com> wrote in message
news:56*************************@posting.google.co m...

Hi,

Turns out it''s possible to spoof another type with Object.GetType
method. If you do this:

namespace N {
class C {
public new Type GetType() {
return (String.Empty.GetType());
}
}
}

and then somewhere in the code you do:

C c = new C();
Console.WriteLine(c.GetType())

the output will be "System.String"

Apparently the way to make it work right is to upcast the reference to
Object:

C c = new C();
Console.WriteLine(((Object)c).GetType())

will yeild "N.C"
I''d appreciate your comments on that.

Yan



Hi Ming Chen,

I have yet very little experience with C#, but isn''t it possible that
you receive a reference to a C object that has a number of subclasses,
and this reference can be pointing to an instance of any of these
subclasses, and you call GetType() to know the actual type but get
some ''spoofed'' value in return?

Thanks,
Yan


这篇关于Object.GetType - 如何正确调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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