简体   繁体   中英

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:

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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