开发者

Case-insensitive GetMethod?

开发者 https://www.devze.com 2023-01-21 00:07 出处:网络
foreach(var filter in filters) { var filterType = typeof(Filters); var method = filterType.GetMethod(filter);
foreach(var filter in filters)
{
    var filterType = typeof(Filters);
    var method = filterType.GetMethod(filter);
    if (开发者_StackOverflow中文版method != null) value = (string)method.Invoke(null, new[] { value });
}

Is there a case-insensitive way to get a method?


Yes, use BindingFlags.IgnoreCase:

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

Beware the possible ambiguity, you'd get an AmbiguousMatchException.


To get a method that acts like GetMethod(filter), except that it ignores the case you need:

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

This will not work: var method = filterType.GetMethod(filter, BindingFlags.IgnoreCase);


Take a look at this variant of GetMethod, specifically note that one of the possible BindingFlags is IgnoreCase.

0

精彩评论

暂无评论...
验证码 换一张
取 消