Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2006

Tip: Looking for answers? Try searching our database.

Type.GetProperty() does not work for private properties?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Bischoff - 13 Nov 2006 14:15 GMT
Hi, I'm trying to set the value of a private property using the following code:

internal void SetProperty(object obj, string propertyName, object newValue)
{
 PropertyInfo pi = obj.GetType().GetProperty(
   propertyName,
   BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

 pi.SetValue(obj, newValue, null);
}

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?

Thanks,
Martin
Mattias Sjögren - 13 Nov 2006 18:19 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.