Unable to get the “Add” method of a ColumnDefinitionCollection in UWP

三世轮回 提交于 2019-12-24 04:04:27

问题


In the context of a Windows Universal Application, and using reflection, I'm trying to get the Add method from a ColumnDefinitionCollection (the built-in type)

So I use this:

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

BUT it returns null!

It also happens with the Contains method. Both of them are defined in ICollection<T> (IList<T> derives from it).

However, if I define my own class deriving from IList<T>, it works perfectly.

Then, how do I get a MethodInfo for the Add method? Is ColumnDefinitionCollection using some tricks? Maybe something related to COM?


回答1:


I really don't know why GetRuntimeMethods is not returning all methods. Is this the expected behavior? Or is it a bug?

Anyway, one solution (or probably a workaround) is to obtain the interfaces that the type implements and then get the methods of these interfaces like this:

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

In your specific case, this will work because Add is actually defined on ICollection<T> which is implemented by ColumnDefinitionCollection.



来源:https://stackoverflow.com/questions/39986903/unable-to-get-the-add-method-of-a-columndefinitioncollection-in-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!