简体   繁体   English

不区分大小写的GetMethod?

[英]Case-insensitive GetMethod?

foreach(var filter in filters)
{
    var filterType = typeof(Filters);
    var method = filterType.GetMethod(filter);
    if (method != null) value = (string)method.Invoke(null, new[] { value });
}

Is there a case-insensitive way to get a method? 是否有一种不区分大小写的方法来获取方法?

Yes, use BindingFlags.IgnoreCase: 是的,使用BindingFlags.IgnoreCase:

var method = filterType.GetMethod(filter, 
    BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

Beware the possible ambiguity, you'd get an AmbiguousMatchException. 注意可能的歧义,你会得到一个AmbiguousMatchException。

To get a method that acts like GetMethod(filter), except that it ignores the case you need: 要获得一个类似于GetMethod(过滤器)的方法,除了忽略您需要的情况:

var method = filterType.GetMethod(filter, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance| BindingFlags.IgnoreCase);

This will not work: var method = filterType.GetMethod(filter, BindingFlags.IgnoreCase); 这不起作用:var method = filterType.GetMethod(filter,BindingFlags.IgnoreCase);

看看GetMethod的这个变种,特别注意其中一个可能的BindingFlagsIgnoreCase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM