c# - 如何通过反射匹配名称和参数类型来获取 protected 方法?

标签 c# asp.net-core .net-core-rc2

在 dotnet core 之前我们能够做到

var member = type.GetMethod(name, bindingFlags, null, argtypes, null);

为了访问与其名称和参数类型匹配的方法,在 dotnet core 中他们删除了这个重载,现在我只能通过 name && binding flags 来获取或name && parameters type ( see ),但与以前不同。

有一个新方法GetRuntimeMethods返回 IEnumerable<MethodInfo>并包括非公共(public)方法,但我无法按参数类型进行过滤。

还有一个方法GetRuntimeMethod我可以按参数类型进行过滤,但它不包括非公共(public)方法。

我已经尝试过类似的方法,但失败了

var member = type.GetRuntimeMethods().Where(m =>
m.Name == name && (m.GetParameters().Select(p => p.GetType()).ToArray() == argtypes)).FirstOrDefault();

有没有办法通过名称和参数类型获取方法?

最佳答案

是的,确实缺少那个重载。您可以使用 GetMethods() 并按照您想要的方式过滤输出。您的尝试很接近,但您无法使用 == 比较数组:

var method = type.GetMethods().FirstOrDefault(m =>
    m.Name == name && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(argTypes));

关于c# - 如何通过反射匹配名称和参数类型来获取 protected 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37708267/

相关文章:

asp.net-core - 在没有 Visual Studio 2015 的情况下配置 .NET Core Web 应用程序启动设置

C#线程行为

c# - 将 IEnumerable ViewModel 传递给 View

c# - quartz 中的计划外工作

c# - 在 asp.net core : Circular dependency problem 的 dbcontext 中播种用户和角色

c# - ASP.NET核心标识: Multiple providers for the same account

azure - HTTPS 不适用于在具有自定义域的 Service Fabric 上运行的 Kestrel 3.2.187/ASPNET Core 2.1.5

c# - .net core 中 StreamReader 的更短解决方案

c# - C# Form 中的 RightToLeft 属性

visual-studio-2015 - 在Visual Studio中还原时的“unknown keyword platform”