BindingFlags.IgnoreCase not working for Type.GetProperty()?
Asked Answered
L

3

263

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?

Legatee answered 5/11, 2008 at 10:5 Comment(7)
@OregonGhost: Does it matter?Fireeater
While your meta question is valid, it doesn't really matter indeed. As most of my questions, my primary reason is the hunger for knowledge ;)Legatee
@leppie: Yes, it does. Maybe there is a use-case for this I am not aware of, and it is always interesting why people want to do things.Worry
@OregonGhost: not all languages targeting .Net are case sensitive, that's why you sometime need to do and case insensitive look-up.Parka
Use case for me: So I can compare objects against a MSSQL Compact Entity without worrying about how they typed the fields. (I am comparing an object against a compact database where some fields are name isSomething and IsSomething.) In other words, for sake of laziness.Hornbook
@OregonGhost: It's been a while since you asked, but, my use case is in a PowerShell Cmdlet (in a module written in C#). I want to allow PowerShell users of my Cmdlet to specify which properties my Cmdlet should consider when it's processing incoming objects (incoming through a pipeline). PowerShell is very case independent.Goldina
This does matter when implementing DynamicObject where the binder has a property for IgnoreCase. If you need to know whether the property exists, for instance when merging json, this makes sense to know if the property exists or if it should go into the dynamic property dictionary.Orthman
P
533

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

Parka answered 5/11, 2008 at 10:9 Comment(5)
any one has any idea why it is like this (asking for knowledge sake ;))Guesswarp
@Guesswarp BindingFlags.Public | BindingFlags.Instance are default flags when you supply only property nameGodfry
@Guesswarp Adding to OtabekKholikov's explanation, if it were to keep these defaults and add (OR) your specified BindingFlags to them, there would be no way to not use the defaults. I.e. It wouldn't be possible to exclude Public properties or to exclude Instance properties. They decided you either take the defaults, or override them by specifying exactly what you're after.Englishman
@Englishman Not quite true, something like BindingFlags.Default & ~BindingFlags.Public would remove the "Public" flag from the default set. Since there are only two default flags this doesn't save any typing really.Alectryomancy
private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; Source : dotnet runtime source codePresage
F
58

You need to add BindingFlags.Public | BindingFlags.Instance

Fireeater answered 5/11, 2008 at 10:7 Comment(3)
You get less up votes, but you were 2 minutes quicker - but then again, Pop's answer had more details. I give votes to all who deserve! :)Eire
@TonyBasallo Not sure why you felt that comment was necessary. In any case, spoon feeding answers teaches people nothing. It's no different than offering nothing more than copy-pasted code that doesn't explain if, how, or why it works in the first place. This is a terrible answer by today's standards.Stomato
It's hilarious that you thought it was necessary to comment on a comment that is 4 years old and make reference to "today's standards" as if they had any bearing on a question/answer/comment from 4 years ago. Normally I would laugh and continue on my day, but this the type of elitist thinking that has made "today's" SO so much less pleasant that 4-years ago SO. Since you're so concerned, I would be lying if I could tell you why I made the comment.Eire
A
14

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.

Aloes answered 22/9, 2009 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.