jeudi 11 octobre 2018

Get interfaces implemented by class

It's my first post on SO. I'm doing assembly analysis project and I encountered a problem.

So what I want to achieve is list of all interfaces implemented by a class, but without derived interfaces (and interfaces implemented by derived classes).

Here's an example to illustrate (from LinqPad, .Dump() is a printing to result window):

void Main()
{
    typeof(A).GetInterfaces().Dump(); //typeof(IT), typeof(IT<Int32>) 
    typeof(B).GetInterfaces().Dump(); //typeof(IT<Int32>) 
    typeof(C).GetInterfaces().Dump(); //typeof(IT), typeof(IT<Int32>) 
}

class C : A {}

class A : IT {}

class B : IT<int> {}

public interface IT : IT <int> {}

public interface IT<T> {}

What I would like to get is

    typeof(A).GetInterfaces().Dump();  //typeof(IT)
    typeof(B).GetInterfaces().Dump();  //typeof(IT<Int32>) 
    typeof(C).GetInterfaces().Dump();  //

I found this post Type.GetInterfaces() for declared interfaces only with and anwser

Type type = typeof(E);
var interfaces = type.GetInterfaces()
    .Where(i => type.GetInterfaceMap(i).TargetMethods.Any(m => m.DeclaringType == type))
    .ToList();

But I'm looking if there is an alternative that iterattining through methods

Is there any way to achieve this?





Aucun commentaire:

Enregistrer un commentaire