Coder Perfect

BindingFlags. For Type, IgnoreCase doesn’t function. GetProperty()?

Problem

Imagine the following

There is a field Company on a type T. It works properly when using the following method:

Type t = typeof(T);
t.GetProperty("Company")

With the call that follows However, I get null.

Type t = typeof(T);
t.GetProperty("company", BindingFlags.IgnoreCase)

Is there anyone who has a suggestion?

Asked by Boris Callens

Solution #1

You’ve overwritten the default look-up flags; if you specify new flags, you’ll need to provide all of the information needed to locate the property. Consider the following scenario: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.IgnoreCase | BindingFlags.Igno

Answered by Pop Catalin

Solution #2

BindingFlags.Public | BindingFlags.Instance must be included.

Answered by leppie

Solution #3

Thank you so much for helping me out in a pinch today. I had audit data recorded, but the property names were incorrectly cased. (Auditing is included into the datalayer.) Anyway, I had to add IgnoreCase as a binding flag, but that didn’t help until my coworker discovered this solution. The following is the resultant function:

public static void SetProperty(Object R, string propertyName, object value)
{
    Type type = R.GetType();
    object result;
    result = type.InvokeMember(
        propertyName, 
        BindingFlags.SetProperty | 
        BindingFlags.IgnoreCase | 
        BindingFlags.Public | 
        BindingFlags.Instance, 
        null, 
        R, 
        new object[] { value });
}

This is a subclass of DotMagic, which I created.

Answered by Josh Warner-Burke

Post is based on https://stackoverflow.com/questions/264745/bindingflags-ignorecase-not-working-for-type-getproperty