Anders Norås [MCAD] wrote:
> J. Jones wrote:
>
>> How do I determine if a type (such as MyContainer) derives from IList?
>
> Use the C# is statement:
> if (MyContainer is IList) {
> // Handle IList implementation
> }
>
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/
MyContainer is a class, not a variable, so that won't compile.
This is ugly, but it works:
using System;
interface IWhatever {}
public class App : IWhatever
{
public static void Main()
{
bool implementsIFace =
ImplementsIFace(typeof(ICloneable), typeof(App));
}
static bool ImplementsIFace(Type interfaceType, Type implementingType)
{
Type[] interfaces = implementingType.GetInterfaces();
foreach(Type t in interfaces)
{
if(t == interfaceType) return true;
}
return false;
}
}
/Joakim