Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetProperty BindingFlags.IgnoreCase wont work without public and Instance in c#

Tags:

c#

reflection

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

If i write the below code it will give null

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

In the mean time if i write this works fine.Why is that so ?

Type t = typeof(T);
t.GetProperty("company", BindingFlags.IgnoreCase|BindingFlags.Public | BindingFlags.Instance)
like image 549
Shrivallabh Avatar asked Feb 18 '13 06:02

Shrivallabh


People also ask

Is Getproperty case sensitive C#?

The search for name is case-sensitive. The search includes public static and public instance properties.

What is BindingFlags C#?

BindingFlags values are used to control binding in methods in classes that find and invoke, create, get, and set members and types. To specify multiple BindingFlags values, use the bitwise 'OR' operator.


1 Answers

The overload which doesn't take BindingFlags effectively defaults to BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance. That's why it finds it in your first snippet.

If you do specify a BindingFlags, you have to specify all the relevant flags - which includes flags to say whether you want to see public members, whether you want to see non-public members, whether you want to see instance members, and whether you want to see static members.

When you just specify BindingFlags.IgnoreCase, you haven't said you want to see any of those, so it won't find anything.

like image 132
Jon Skeet Avatar answered Oct 07 '22 00:10

Jon Skeet