无法在 UWP 中获取 ColumnDefinitionCollection 的 "Add" 方法

Unable to get the "Add" method of a ColumnDefinitionCollection in UWP

在 Windows 通用应用程序的上下文中,并使用反射,我试图从 ColumnDefinitionCollection(内置类型)中获取 Add 方法

所以我用这个:

type.GetRuntimeMethods().First(info => info.Name == "Add");

但是它 returns 无效!

Contains 方法也会发生这种情况。它们都在 ICollection<T> 中定义(IList<T> 派生自它)。

但是,如果我定义自己的 class 从 IList<T> 派生,它会完美地工作。

那么,如何获取 Add 方法的 MethodInfo? ColumnDefinitionCollection是不是在耍花招?也许与 COM 有关?

我真的不知道为什么 GetRuntimeMethods 没有返回所有方法。这是预期的行为吗?或者这是一个错误?

无论如何,一种解决方案(或者可能是一种解决方法)是获取该类型实现的接口,然后像这样获取这些接口的方法:

var methodsOfImplementedInterfaces = 
    type
        .GetTypeInfo()
        .ImplementedInterfaces
        .SelectMany(x => x.GetRuntimeMethods())
        .ToList();

在您的特定情况下,这会起作用,因为 Add 实际上是在 ICollection<T> 上定义的,而 ICollection<T> 是由 ColumnDefinitionCollection 实现的。