mpen
mpen

Reputation: 283163

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?

Upvotes: 18

Views: 6846

Answers (3)

Hans Passant
Hans Passant

Reputation: 942109

Yes, use BindingFlags.IgnoreCase:

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

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

Upvotes: 33

jyoung
jyoung

Reputation: 5181

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

Upvotes: 5

Logan Capaldo
Logan Capaldo

Reputation: 40346

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

Upvotes: 2

Related Questions