methodBase将是类的方法,而不是接口.您需要在界面上查找相同的方法.在C#中,这有点简单(因为它必须像命名一样),但你需要考虑显式实现之类的东西.如果你有VB代码,那将会比较棘手,因为VB方法"Foo"可以实现一个接口方法"Bar".为此,您需要调查接口映射:
using System; using System.ComponentModel; using System.Diagnostics; using System.Reflection; interface IFoo { void AAA(); // just to push Bar to index 1 [Description("abc")] void Bar(); } class Foo : IFoo { public void AAA() { } // just to satisfy interface static void Main() { IFoo foo = new Foo(); foo.Bar(); } void IFoo.Bar() { GetAttribute(); } void GetAttribute() { // simplified just to obtain the [Description] StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(1); MethodBase classMethod = stackFrame.GetMethod(); InterfaceMapping map = GetType().GetInterfaceMap(typeof(IFoo)); int index = Array.IndexOf(map.TargetMethods, classMethod); MethodBase iMethod = map.InterfaceMethods[index]; string desc = ((DescriptionAttribute)Attribute.GetCustomAttribute(iMethod, typeof(DescriptionAttribute))).Description; } }