比较对象类型 [英] Comparing object types

查看:68
本文介绍了比较对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定两个对象是否属于同一类型的最佳方法是什么?

''''运算符不够精确。目前我正在使用

Obj1.GetType()。Name == Obj2.GetType()。名称但在我看来必须有一个更好的方式

。以下代码更好地了解了这种情况:


抽象类A

{

//参数可能是实例任何定义的类

static void TestFunc(一个obj1,一个obj2)

{

if(obj1.GetType()。Name == obj2.GetType()。姓名)

{... //一些代码}

其他

{... //做别的事情}

}

}

等级B {}:A

等级B2 {}:A

class C {}:B


只是一个想法:将(Obj1是Obj2&& Obj2是Obj1)更好地工作并且更加

有效吗?


- Ken

What is the best way to determine if two objects are of the same type? The
''is'' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be
a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more
efficient?

--Ken

推荐答案

Kenneth Baltrinic写道:
Kenneth Baltrinic wrote:
只是一个想法:将(Obj1是Obj2&& Obj2是Obj1)工作得更好并且
更有效率?
Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and
be more efficient?




我跟不上你。为什么两个测试?如果其中一个是真的那么另一个。


-

有10种人。那些懂二元的人和那些没有b $ b的人。

http://code.acadx.com

(拉针回复)



I''m not following you. Why two tests? If either is true so is the other.

--
There are 10 kinds of people. Those who understand binary and those who
don''t.

http://code.acadx.com
(Pull the pin to reply)


嗨Kenneth,
Obj1.GetType()。Name == Obj2.GetType()。名称但在我看来必须
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must



be


Obj1.GetType()== Obj2GetType()

每种类型只有一个Type-class实例。因此,如果对象是
,则相同类型的GetType方法将返回相同的引用,而不管

调用GetType方法的对象。

HTH

B \ rgds



be

Obj1.GetType() == Obj2GetType()

There is only one Type-class instance per type. Thus, if the objects are of
the same type GetType method will return the same reference regardless of
the object which GetType method is called.

HTH
B\rgds
100




Kenneth Baltrinic <柯***** @ baltrinic.com>在消息中写道

新闻:ek ************** @ TK2MSFTNGP11.phx.gbl ...

"Kenneth Baltrinic" <ke*****@baltrinic.com> wrote in message
news:ek**************@TK2MSFTNGP11.phx.gbl...
最好的方法是什么?确定两个对象是否属于同一类型?
''是''运算符不够精确。目前我正在使用
Obj1.GetType()。Name == Obj2.GetType()。名称,但在我看来必须
是一个更好的方法。以下代码更好地了解了这种情况:

抽象类A
{
//参数可以是任何类定义的实例
static void TestFunc(一个obj1,一个obj2)
{
if(obj1.GetType()。Name == obj2.GetType()。Name)
{... // some code}
其他
{... //做别的事情}
}
}
B类{}:一个类B2 {}:A
C类{}:B

只是一个想法:会(Obj1是Obj2&& Obj2是Obj1)工作得更好而且
效率更高?

--KEN
What is the best way to determine if two objects are of the same type? The ''is'' operator is not precise enough. Currently I am using
Obj1.GetType().Name == Obj2.GetType().Name but it seems to me there must be a better way. The following code gives a better idea of the situation:

abstract class A
{
//the arguments could be instances of any of the classes defined
static void TestFunc(A obj1, A obj2)
{
if(obj1.GetType().Name == obj2.GetType().Name)
{ ... //some code}
else
{ ... // do something else }
}
}
class B {} : A
class B2 {} : A
class C {} : B

Just a thought: would (Obj1 is Obj2 && Obj2 is Obj1) work better and be more efficient?

--Ken




在java中只有一个对象类为每个不同的类创建,在

C#my猜测是每个不同的类型只创建一个类型,

所以在一个类型上调用.Equals应该总是给你正确的答案,对于

例如这打印为true :

Console.WriteLine(" asd" .GetType()。Equals(" a) sd1222" .GetType()));



in java there is only one object Class created for each different class, in
C# my guess is that there is only one Type created for each different type,
so calling .Equals on a type should always give you the correct answer, for
example this prints true:
Console.WriteLine("asd".GetType().Equals("asd1222" .GetType()));


这篇关于比较对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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