lundi 5 septembre 2016

Base interface in c#

I need some sort of way to mark base interfaces and identify if a class implemented the base interface or its derived interface. c# doesn't allow having 'abstract interface'. Is there any way to do this in c#?

public interface IBaseFoo
{
     void BaseMethod();
}

public interface IFoo : IBaseFoo
{
     void FooMethod();
}

public class Base
{

}

public class A : Base, IFoo
{

}

public class B : Base, IBaseFoo
{

}

Now in the following method I need to check if the typeCls is implemented the IFoo or IBaseFoo without explicitly specifying types. I need sort of a way to mark the base interface and identify it in the method. (ie: if c# allowed having abstract interface, I could have check if IsAbstract property of interfaces of typeClas)

public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
     // Here I need to check if the typeCls is implemented the IFoo or IBaseFoo
}





Aucun commentaire:

Enregistrer un commentaire