tags:

views:

105

answers:

3

I am working with two types, one generic and the other not. I don't have instances of objects but I want to find out if ( MyType is T ) or in other words if ( MyType inherits T)

Again, I am looking for:

if ( Truck is Vehicle )

not

if ( MyTruckObject is Vehicle)
+5  A: 

try:

if (typeof(Truck).IsSubclassOf(typeof(Vehicle)))
David Radcliffe
Pretty darn close! Thanks!
Nate Zaugg
+11  A: 

Type.IsSubclassOf

Stephen Cleary
+2  A: 

Well, given a generic type argument, you could do something like:

if (typeof(Vehicle).IsAssignableFrom(typeof(T))) 
{

}

Or, apply a constraint to a method to ensure it:

public void DoSomething<T>() where T : Vehicle 
{

}
Matthew Abbott