Martin,
>The problem is, that Type.GetProperty() returns null if propertyName is the
>name of a private property.
>Shouldn't GetProperty() also return private properties when
>BindingFlags.NonPublic is specified?
Yes, and it works fine here. The property isn't static, is it? Can you
post a complete application that will reproduce the problem?
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Martin Bischoff - 14 Nov 2006 08:25 GMT
Hi Mattias, thanks for your reply.
I forgot to post one important fact: the private property I'm looking for is
not defined in a base class of the object passed to the SetProperty() method.
Type.GetProperty() does not return private properties declared in base
types. So my modified (and working) method now also checks the base types:
void SetProperty(object obj, string propertyName, object newValue)
{
Type type = obj.GetType();
PropertyInfo pi = null;
while ((pi == null) && (type != typeof(object)))
{
pi = type.GetProperty(propertyName,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.FlattenHierarchy);
type = type.BaseType;
}
if (pi != null)
pi.SetValue(obj, newValue, null);
}
Best regards,
Martin
> Martin,
>
[quoted text clipped - 7 lines]
>
> Mattias