在类型上查找立即实现的接口

在以下场景中调用typeof(Bar).GetInterfaces()时,该方法返回IFoo和IBar。

interface IFoo {}
interface IBar : IFoo {}
class Bar : IBar {}

有没有办法可以在Bar上找到直接界面(IBar)?

不,在编译的代码中没有“立即”接口这样的东西。 您的课程有效编译为:

 class Bar : IBar, IFoo { } 

你无法区分这两者。 您唯一能做的就是检查所有这些接口,看看两个或多个接口是否相互inheritance(即使在这种情况下,您也无法真正检查该类的作者是否已明确提及代码中的基接口是不是):

 static IEnumerable GetImmediateInterfaces(Type type) { var interfaces = type.GetInterfaces(); var result = new HashSet(interfaces); foreach (Type i in interfaces) result.ExceptWith(i.GetInterfaces()); return result; } 
 public interface IRoo { } public interface ISoo : IRoo { } public interface IMoo : ISoo { } public interface IGoo : IMoo { } public interface IFoo : IGoo { } public interface IBar : IFoo { } public class Bar : IBar { } private void button1_Click(object sender, EventArgs e) { Type[] interfaces = typeof(Bar).GetInterfaces(); Type immediateInterface = GetPrimaryInterface(interfaces); // IBar } public Type GetPrimaryInterface(Type[] interfaces) { if (interfaces.Length == 0) return null; if (interfaces.Length == 1) return interfaces[0]; Dictionary typeScores = new Dictionary(); foreach (Type t in interfaces) typeScores.Add(t, 0); foreach (Type t in interfaces) foreach (Type t1 in interfaces) if (t.IsAssignableFrom(t1)) typeScores[t1]++; Type winner = null; int bestScore = -1; foreach (KeyValuePair pair in typeScores) { if (pair.Value > bestScore) { bestScore = pair.Value; winner = pair.Key; } } return winner; } 

这将选择具有最长inheritance树的接口。

 typeof(Bar) .GetInterfaces() .OrderByDescending(i => i.GetInterfaces().Length) .FirstOrDefault() 

这对我的用例来说已经足够了。