vendredi 25 août 2017

How to get the interface Methodinfo, based on concrete implementation's MethodInfo?

I have an interface IClass:

public interface IClass
{
    [MyAttribute]
    int Hello();

    [MyAttribute]
    int Farewell();
}

And an implementation Class1:

public class Class1 IClass
{
    public int Hello() { Console.WriteLine("Hi there") };

    int IClass.Farewell() { Console.WriteLine("Goodbye!") };
}

I have a handle on Class1's MethodInfo's for Hello() and IClass.Farewell() by means of interception, but I need to find the MethodInfo of the interface definition in order to discover the usage of the attribute MyAttribute; something like this:

MethodInfo mi = context.Descriptior.CurrentMethod;
MethodInfo imi = GetInterfaceMethodInfo(mi) // ???
var mas = GetCustomAttributes<MyAttribute>();
if (mas.Count > 0)
{
    // Profit!
}





Aucun commentaire:

Enregistrer un commentaire