Assume the following type definitions:
public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
How do I find out whether the type Foo
implements the generic interface IBar<T>
when only the mangled type is available?
Assume the following type definitions:
public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
How do I find out whether the type Foo
implements the generic interface IBar<T>
when only the mangled type is available?
You have to check against a constructed type of the generic interface.
You will have to do something like this:
foo is IBar<String>
because IBar<String>
represents that constructed type. The reason you have to do this is because if T
is undefined in your check, the compiler doesn't know if you mean IBar<Int32>
or IBar<SomethingElse>
.
You have to go up through the inheritance tree and find all the interfaces for each class in the tree, and compare typeof(IBar<>)
with the result of calling Type.GetGenericTypeDefinition
if the interface is generic. It's all a bit painful, certainly.
See this answer and these ones for more info and code.
public interface IFoo<T> : IBar<T> {}
public class Foo : IFoo<Foo> {}
var implementedInterfaces = typeof( Foo ).GetInterfaces();
foreach( var interfaceType in implementedInterfaces ) {
if ( false == interfaceType.IsGeneric ) { continue; }
var genericType = interfaceType.GetGenericTypeDefinition();
if ( genericType == typeof( IFoo<> ) ) {
// do something !
break;
}
}
First of all public class Foo : IFoo<T> {}
does not compile because you need to specify a class instead of T, but assuming you do something like public class Foo : IFoo<SomeClass> {}
then if you do
Foo f = new Foo();
IBar<SomeClass> b = f as IBar<SomeClass>;
if(b != null) //derives from IBar<>
Blabla();
By using the answer from TcKs it can also be done with the following LINQ query:
bool isBar = foo.GetType().GetInterfaces().Any(x =>
x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(IBar<>));
As a helper method extension
public static bool Implements<I>(this Type type, I @interface) where I : class
{
if(((@interface as Type)==null) || !(@interface as Type).IsInterface)
throw new ArgumentException("Only interfaces can be 'implemented'.");
return (@interface as Type).IsAssignableFrom(type);
}
example usage:
var testObject = new Dictionary<int, object>();
result = testObject.GetType().Implements(typeof(IDictionary<int, object>)); // true!