We help IT Professionals succeed at work.
Get Started

Reflection in .Net  1.1

611 Views
Last Modified: 2013-11-25
I'm working on a problem that requires using reflection to obtain values from the properties of an object.  I've run into a small glitch that I'm hoping to find some answers to.

Given an object with a number of both public and private properties I can call the Type.GetProperty(<name>, BindingFlags) and retrieve the values for all of the properties.

Now if I create a new class that inherits the previously mentioned class and then try to retrieve both the Public and Private properties, only the Public properties are returned.

The output from the attached code snippet is:

someOtherPublicProp
someOtherPrivateProp
somePublicProp

But I would expect to see this:

someOtherPublicProp
someOtherPrivateProp
somePublicProp
somePrivateProp

Notice that the public properties are displayed for both classes however the private properties are only displayed for the inheritting class, not from the class it inherits from.  

What am I missing?
Private Sub TestProps()
        Dim o As New foo
        For Each pi As PropertyInfo In _
            o.GetType.GetProperties(BindingFlags.Instance Or _
                                    BindingFlags.Static Or _
                                    BindingFlags.IgnoreCase Or _
                                    BindingFlags.NonPublic Or _
                                    BindingFlags.Public Or _
                                    BindingFlags.FlattenHierarchy)
            Console.WriteLine(pi.Name)
        Next
    End Sub
 
    Class foo
        Inherits bar
        Public ReadOnly Property someOtherPublicProp() As String
            Get
                Return "someOtherPublicProp"
            End Get
        End Property
        Private ReadOnly Property someOtherPrivateProp() As String
            Get
                Return "someOtherPrivateProp"
            End Get
        End Property
    End Class
 
    Class bar
        Public ReadOnly Property somePublicProp() As String
            Get
                Return "somePublicProp"
            End Get
        End Property
        Private ReadOnly Property somePrivateProp() As String
            Get
                Return "somePrivateProp"
            End Get
        End Property
    End Class

Open in new window

Comment
Watch Question
Software Architect
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE