471,318 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Is there an easier or better way to compare Types?

AssumeSomeFunctionExists ( Type someType, Type someOtherType)
{

// I want to know
// 1. Are the two the same Type
// 2. Are they members of the same Inheritance branch
// a) Is someType a subclassof someOtherType or
// b) Is someOtherType a subclass of someType
// This code does work but...

if ( someType.Equals(someOtherType) ||
someType.IsSubclassOf(someOtherType) || someOtherType.IsSubclassOf (
someType) )

// ... doSomething

}

Now I know about the "is" operator but that only works with an object
instance like

if ( anObject is SomeClass )

which does exactly what I want but I cannot use the "is" operator to compare
two Type objects

And I know about IsSubclass() but that does not return 'true' when the two
are exactly the same. So I
do what I show in the above code which takes three comparisons. Am I
missing a shortcut way to do this?

Thanks a lot in advance

JIM

Feb 23 '06 #1
4 4892
Bob
Hi James,
I usually just work with the name property.
if (GetType(someType).Name="MyBase")

Don't know how efficient it is.

FWIW

Bob

"james" <no****@hypercon.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
AssumeSomeFunctionExists ( Type someType, Type someOtherType)
{

// I want to know
// 1. Are the two the same Type
// 2. Are they members of the same Inheritance branch
// a) Is someType a subclassof someOtherType or
// b) Is someOtherType a subclass of someType
// This code does work but...

if ( someType.Equals(someOtherType) ||
someType.IsSubclassOf(someOtherType) || someOtherType.IsSubclassOf (
someType) )

// ... doSomething

}

Now I know about the "is" operator but that only works with an object
instance like

if ( anObject is SomeClass )

which does exactly what I want but I cannot use the "is" operator to compare two Type objects

And I know about IsSubclass() but that does not return 'true' when the two
are exactly the same. So I
do what I show in the above code which takes three comparisons. Am I
missing a shortcut way to do this?

Thanks a lot in advance

JIM

Feb 23 '06 #2
james <no****@hypercon.net> wrote:

<snip>
And I know about IsSubclass() but that does not return 'true' when the two
are exactly the same. So I
do what I show in the above code which takes three comparisons. Am I
missing a shortcut way to do this?


Have a look at Type.IsAssignableFrom. You'd just have to try it two
ways.

--
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
Feb 23 '06 #3
That will tell me if they are equal, but nothing else unless I misunderstand
your answer

JIM

"Bob" <bo*@nowhere.com> wrote in message
news:eX*************@TK2MSFTNGP15.phx.gbl...
Hi James,
I usually just work with the name property.
if (GetType(someType).Name="MyBase")

Don't know how efficient it is.

FWIW

Bob

"james" <no****@hypercon.net> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
AssumeSomeFunctionExists ( Type someType, Type someOtherType)
{

// I want to know
// 1. Are the two the same Type
// 2. Are they members of the same Inheritance branch
// a) Is someType a subclassof someOtherType or
// b) Is someOtherType a subclass of someType
// This code does work but...

if ( someType.Equals(someOtherType) ||
someType.IsSubclassOf(someOtherType) || someOtherType.IsSubclassOf (
someType) )

// ... doSomething

}

Now I know about the "is" operator but that only works with an object
instance like

if ( anObject is SomeClass )

which does exactly what I want but I cannot use the "is" operator to

compare
two Type objects

And I know about IsSubclass() but that does not return 'true' when the
two
are exactly the same. So I
do what I show in the above code which takes three comparisons. Am I
missing a shortcut way to do this?

Thanks a lot in advance

JIM


Feb 23 '06 #4
Thanks Jon, I don't remember seeing that one before.. I'll give it a whirl
JIM
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
james <no****@hypercon.net> wrote:

<snip>
And I know about IsSubclass() but that does not return 'true' when the
two
are exactly the same. So I
do what I show in the above code which takes three comparisons. Am I
missing a shortcut way to do this?


Have a look at Type.IsAssignableFrom. You'd just have to try it two
ways.

--
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

Feb 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

73 posts views Thread by RobertMaas | last post: by
4 posts views Thread by slot | last post: by
11 posts views Thread by Russ Green | last post: by
3 posts views Thread by Kiran B. | last post: by
7 posts views Thread by Prabhudhas Peter | last post: by
4 posts views Thread by =?Utf-8?B?TmlrbGFz?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.