Let’s assume type MyType implements interface IMyInterface. How to find type declaring an interface ? For example,
class UnitTest
{
MyTypeBase : IMyInterface { }
MyType : MyTypeBase { }
void Test() {
Type declaration = FindDeclaration(typeof(MyType), typeof(IMyInterface));
Assert.AreEqual(typeof(MyTypeBase), declaration)
}
You need to use the
GetInterfaceorGetInterfacesmethods of theType.For example you could do this:
Or you could call
GetInterfacesand loop through the results until you findIMyInterface.Sources:
Type.GetInterface(string)
Type.GetInterfaces()