lundi 30 avril 2018

Using C# reflection to find interface declaring the function

I am looking for an easy way to get the reflection information on the method starting all the way from the class, and going back all the way to the declaring interface. Here is the simplified piece of code:

public interface Ix
{
    void Function();
}
public class X : Ix
{
    public void Function() {}
}

public class Y : X
{
}

Method info for class X has no information about Function being declared in Ix interface. Here is the call:

var info = typeof(X).GetMethod("Function");
var baseInfo = info.GetBaseDefinition()

It returns the following data:

info.DeclaringType --> MyNamespace.X
info.ReflectedType --> MyNamespace.X
baseInfo.DeclaringType --> MyNamespace.X
baseInfo.ReflectedType --> MyNamespace.X

The same information is returned for class Y.

How can I figure out that this Function was declared in interface Ix without going through all implemented interfaces and base classes of class X or Y? I could be missing something simple but I cannot figure out what. Could this be a bug?

This is .Net Core SDK version 2.1.104 and Visual Studio 2017





Aucun commentaire:

Enregistrer un commentaire