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();
}
}
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 , .
.