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 / March 2008

Tip: Looking for answers? Try searching our database.

Retrieving all properties by reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edgile - 17 Mar 2008 10:44 GMT
Hi,

I would like to retrieve all properties of a type with the following
conditions:
- should include private properties, also from base classes
- polymorphic properties should be there only once
- static properties are not wanted

Algoritm 1:
Very simple: type.GetProperties(Public | NonPublic | Instance)
Problem: This one does not return the private members of base classes

Algoritm 2:
while(currentType != typeof(object)
{
   currentType.GetProperties(DeclaredOnly | Public | NonPublic | Instance)
   currentType = currentType.BaseType
}

Problem: This one duplicates overridden polymorphic properties, and I found
no way to filter them out. If I match by name, I might also delete private
properties of base types that have the same name. I cannot differenciate
properties of the same name via access modifier since all private/
protected/internal properties are 'non-public' and no further details
PropertyInfo class provides.

Algorithm 3:
step1: type.GetProperties(Public | NonPublic | Instance); // Getting all
execpt private ones of base classes.
step2: getting the private ones of base types
currentType = type.BaseType;
while(currentType != typeof(object)
{
   currentType.GetProperties(DeclaredOnly | NonPublic | Instance);
   currentType = currentType.BaseType;
}

Problem: suffers from the same defects as Algorithm 2: in step 2, I cannot
distinguish protected and private properties.

Any ideas?
Ben Voigt [C++ MVP] - 17 Mar 2008 18:12 GMT
> Hi,
>
[quoted text clipped - 17 lines]
> since all private/ protected/internal properties are 'non-public' and
> no further details PropertyInfo class provides.

Try checking DeclaringType == currentType.  You may have to check on the
accessor methods instead of the property itself, because virtual property
access is done through virtual accessor methods, the property itself always
hides the base class version.
Edgile - 18 Mar 2008 16:57 GMT
Hi Ben,

Thanx for the help your tip finally worked. Although I did not compare
DeclaringType, but the accessor methods do expose their access modifiers, so
I could easily filter the private ones. Thanx again, Ben!

> > Hi,
> >
[quoted text clipped - 22 lines]
> access is done through virtual accessor methods, the property itself always
> hides the base class version.
Ben Voigt [C++ MVP] - 18 Mar 2008 18:41 GMT
> Hi Ben,
>
> Thanx for the help your tip finally worked. Although I did not compare
> DeclaringType, but the accessor methods do expose their access
> modifiers, so I could easily filter the private ones. Thanx again,
> Ben!

You're welcome :)

>>> Hi,
>>>
[quoted text clipped - 22 lines]
>> property access is done through virtual accessor methods, the
>> property itself always hides the base class version.

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.