dinsdag 20 januari 2009

determine interfaces

I had the next problem: I had a type T (in a template) and I wanted to check if it implemented a certain interface. I finally managed it the next way:

if (typeof(<InterfaceName>).IsAssignableFrom(typeof(T)))

{

    // .. some code

}

1 opmerking:

Edwin van den Belt zei

I found another way to archive the same thing. The method mentioned above doesn't work in all situations.

typeof(<object>).GetInterfaces("<interfacename>") != null seems to work also