Is there an easier or better way to compare Types?

  • Thread starter Thread starter james
  • Start date Start date
J

james

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
 
Hi James,
I usually just work with the name property.
if (GetType(someType).Name="MyBase")



Don't know how efficient it is.

FWIW

Bob
 
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.
 
That will tell me if they are equal, but nothing else unless I misunderstand
your answer

JIM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top