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

403

Solution 1

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

You need to add BindingFlags.Public | BindingFlags.Instance

Solution 3

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.

Share:
403

Related videos on Youtube

user308102
Author by

user308102

Updated on November 22, 2021

Comments

  • user308102
    user308102 over 2 years

    I have a data table called movieTitles and each field is a title of a movie. Under each field I will be putting keywords for each movie (a word for each row), and I want to just insert those words into the field without having to query the rest of the fields.

    EX.

    I will be having thousands of movie titles as the field name and I will have a interface that will take words in for one movie at a time. so Lets add the words [legos, lord business, benny, will ferrell] to the field: Lego Movie. But I will be having thousands of movies and don't want to have the query:


    INSERT INTO table(Fury, Lego Movie, Fight Club, Step Brothers, Anchorman) VALUES ('','legos','','','')
    
    INSERT INTO table(Fury, Lego Movie, Fight Club, Step Brothers, Anchorman) VALUES ('','lord business','','','')
    
    INSERT INTO table(Fury, Lego Movie, Fight Club, Step Brothers, Anchorman) VALUES ('','benny','','','')
    
    INSERT INTO table(Fury, Lego Movie, Fight Club, Step Brothers, Anchorman) VALUES ('','will ferrell','','','')
    

    Is there some way that I can just add one word to a field like:


    INSERT INTO table(Lego Movie) VALUE ('legos')
    
    INSERT INTO table(Lego Movie) VALUE ('lord business')
    
    INSERT INTO table(Lego Movie) VALUE ('benny')
    
    INSERT INTO table(Lego Movie) VALUE ('will ferrell')
    

    I know I can do a bunch of nested loops and add buffers and stuff like that but I feel like there should be something real simple. Anyone have an idea?

    • leppie
      leppie over 15 years
      @OregonGhost: Does it matter?
    • Boris Callens
      Boris Callens over 15 years
      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 ;)
    • OregonGhost
      OregonGhost over 15 years
      @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.
    • Pop Catalin
      Pop Catalin over 15 years
      @OregonGhost: not all languages targeting .Net are case sensitive, that's why you sometime need to do and case insensitive look-up.
    • teynon
      teynon almost 11 years
      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.
    • tadman
      tadman over 9 years
      Everything about this database is completely wrong from a database normalization perspective. Why would you do it this way?
    • Flydog57
      Flydog57 about 5 years
      @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.
  • Shrivallabh
    Shrivallabh about 11 years
    any one has any idea why it is like this (asking for knowledge sake ;))
  • Tony Basallo
    Tony Basallo almost 6 years
    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! :)
  • Otabek Kholikov
    Otabek Kholikov about 5 years
    @Shrivallabh BindingFlags.Public | BindingFlags.Instance are default flags when you supply only property name
  • xr280xr
    xr280xr almost 4 years
    @Shrivallabh 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.
  • Slight
    Slight about 3 years
    @xr280xr 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.
  • NullPointerWizard
    NullPointerWizard over 2 years
    private const BindingFlags DefaultLookup = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public; Source : dotnet runtime source code