Case-insensitive GetMethod?
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
.
精彩评论