How to compare types by variables

  • Thread starter Thread starter Yahoo
  • Start date Start date
Y

Yahoo

I am trying to determine if an object inherits a given interface at runtime
and cant figure out how to do it.

if we knew the interface type at design time...
if (x is typeof(InterfaceName))

but how do we do it when we dont know the interface until runtime.
What a want to do...
void myfx(Type type)
if (x is typeof(type)) (ILLEGAL)

What also doesnt work...
if (x.getType() == type) (wont compare inherited types)
 
Have you tried using the following:
if (x is typeA)
{
}

This will evaluate to true, if x is of type typeA or any derived class of
typeA.

--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
That doesnt seem to work. I get the following error 'the type or namespace
name 'type' could not be found'

void myfx(Type type)
if (x istype) (/*ILLEGAL*/)
 
Ah. I misundersood what you were trying to do. You want to pass the type to
a function which compares the object type to the passed type and return the
result. Does this code compile for you?
static void MyFX(object o, Type type)

if (o.GetType() == type)
--
- Shuvro
SDE, MSFT


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
yes, this works. but the problem is that it does not compare inheritance
where an 'is' statement would. This is critical because I want to see it if
implements the passed in interface (not class). 'i' is an interface, 'o' is
an object that implements i. MyFX(o,i) would return false because o would
return the type of 'o' and i would be the type of the 'i', they are not
equal.

The problem i am trying to solve is that i have a collection of items that
all implement a given interface, these objects typically also implement
specialized interfaces of the main interface. I want to single out a
specialized interface specified in an xml config file.

Thanks
Joe
 
Yahoo said:
I am trying to determine if an object inherits a given interface at runtime
and cant figure out how to do it.

if we knew the interface type at design time...
if (x is typeof(InterfaceName))

but how do we do it when we dont know the interface until runtime.
What a want to do...
void myfx(Type type)
if (x is typeof(type)) (ILLEGAL)

What also doesnt work...
if (x.getType() == type) (wont compare inherited types)

Use if (type.IsAssignableFrom(x.GetType()))
 
Yahoo said:
I am trying to determine if an object inherits a given interface at runtime
and cant figure out how to do it.

if we knew the interface type at design time...
if (x is typeof(InterfaceName))

but how do we do it when we dont know the interface until runtime.
What a want to do...
void myfx(Type type)
if (x is typeof(type)) (ILLEGAL)

What also doesnt work...
if (x.getType() == type) (wont compare inherited types)

try
if (type.IsInstanceOfType(x))

Christof
 
Back
Top