views:

1718

answers:

6

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?

+1  A: 

You need to add BindingFlags.Public | BindingFlags.Instance

leppie
+22  A: 

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

Pop Catalin
Ic. Didn't realise that. Thanks.
borisCallens
Thanks. This helped me as well. Hurray for google finding your answer! +1
Erik van Brakel
A: 

Exactly what i was looking for.

Thanx !

A: 

Hi, Thanks. It works...but I was wondering ...why. I can't realize where I've overwritten the default look-up flags. Could you give me some tips? Thanks in advance.

your override the default flags when you specify another set, the override is only for the said method call.
Pop Catalin
A: 

What Pop Catalin means is if you don't specify any flags the .NET passes some default flags in but if you pass in a flag of your own, .NET does not pass any default flags hence meaning that default flags have been overwritten. This is because when you skip any of the parameters, it calls an overload which in the end might call the same method but with some default parameters.

A: 

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.

Josh Warner-Burke