How to reflect the implementation of the explicit C # interface from the call stack?

Is it possible to think of an explicit implementation of the interface from the call stack? I want to use this information to search for an attribute on the interface itself.

Given this code:

interface IFoo
{
    void Test();    
}

class Foo : IFoo
{
    void IFoo.Test() { Program.Trace(); }
}

class Program
{
    static void Main(string[] args)
    {
        IFoo f = new Foo();
        f.Test();
    }

    public static void Trace()
    {
        var method = new StackTrace(1, false).GetFrame(0).GetMethod();
        // method.???
    }
}

In particular, in Trace () I would like to be able to get to typeof(IFoo)from method.

In the viewport, if I look at method.ToString(), it gives me Void InterfaceReflection.IFoo.Test()(InterfaceReflection is the name of my assembly).

How can I get to typeof(IFoo)from there? Should I use a type search based on the name from the assembly itself or is Type IFooit hidden somewhere in MethodBase?

UPDATE:

Here's the final solution, thanks to Kyte

public static void Trace()
{
    var method = new StackTrace(1, false).GetFrame(0).GetMethod();
    var parts = method.Name.Split('.');
    var iname = parts[parts.Length - 2];
    var itype = method.DeclaringType.GetInterface(iname);
}

itype . , , . itype , .

.

+5
4

VS2010, DeclaringType, , , .

    public static void Trace() {
        var stack = new StackTrace(1, true);
        var frame = stack.GetFrame(0);
        var method = frame.GetMethod();

        var type = method.DeclaringType;

        Console.WriteLine(type);
        foreach (var i in type.GetInterfaces()) {
            Console.WriteLine(i);
        }
    }

:

TestConsole.Foo
TestConsole.IFoo

( TestConsole)

+3

method System.Reflection.RuntimeMethodInfo, , System.Reflect.MethodBase. , , Invoke() (, , , , ).

ToString() . InterfaceReflection?

, .

: , . , DeclaringType, , ( , , ):

, Type Foo.

, , . , , , " , "? .

, , , GetInterfaces() Type , DeclaringType, , .

+2

, , , Foo Program . , , "" Foo ( ) , , , ... , , Foo "" ( , , , , ). ( Program.Trace()), "" Foo ( ).

, scenerio, , .

ETA: :

public interface IFoo
{
    event EventHandler Testing;
    void Test();
}
public class Foo : IFoo
{
    public event EventHandler Testing;
    protected void OnTesting(EventArgs e)
    {
        if (Testing != null)
            Testing(this, e);
    }
    public void Test()
    {
        OnTesting(EventArgs.Empty);
    }
}

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        IFoo f = new Foo();
        f.Testing += new EventHandler(f_Testing);
        f.Test();
    }

    static void f_Testing(object sender, EventArgs e)
    {
        IFoo foo = sender as IFoo;
        if (foo != null)
        { 
            //...
        }
    }
}

.

0

, .NET MethodInfo.Name, . :

interface IFoo
{
    void Test();
}
interface IFoo2
{
    void Test();
}

class Foo : IFoo, IFoo2
{
    void IFoo.Test() { Trace(); }
    void IFoo2.Test() { Trace(); }
}

typeof(Foo).GetMethods() Test(), , , , ?

MethodInfo.DeclaringType , . , IFoo , , .DeclaringType .

, MethodInfo, , , , :

    public static void Trace()
    {
        var method = new System.Diagnostics.StackTrace(1, false).GetFrame(0).GetMethod();
        var fromType = method.DeclaringType;
        if (method.Name.Contains("."))
        {
            var iname = method.Name.Substring(0, method.Name.LastIndexOf('.'));
            fromType = Type.GetType(iname); // fromType is now IFoo.
        }
    }
0

All Articles