BindingFlags.IgnoreCase not working for Type.GetProperty()?

C#ReflectionGetpropertyBindingflags

C# Problem Overview


Imagine the following

A type T has a field Company. When executing the following method it works perfectly:

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

Whith the following call I get null though

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

Anybody got an idea?

C# Solutions


Solution 1 - C#

You've overwritten the default look-up flags, if you specify new flags you need to provide all the info so that the property can be found. For example: BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance

Solution 2 - C#

You need to add BindingFlags.Public | BindingFlags.Instance

Solution 3 - C#

Thanks, this really helped me out in a pinch today. I had audit information saved, but with incorrect casing on the property names. (The auditing is built into a datalayer.) Anyway so I had to add IgnoreCase as a binding flag, but then it still didn't work, till my coworker found this answer. The resulting 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 part of a class I call DotMagic.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBoris CallensView Question on Stackoverflow
Solution 1 - C#Pop CatalinView Answer on Stackoverflow
Solution 2 - C#leppieView Answer on Stackoverflow
Solution 3 - C#Josh Warner-BurkeView Answer on Stackoverflow