Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:40:06+00:00 2026-06-07T19:40:06+00:00

How can I get base class properties and fields using reflection, so that I

  • 0

How can I get base class properties and fields using reflection, so that I can work UP the class hierarchy one level at a time? The goal is to build a tree display showing properties and fields with values of any class instance, just like the debugger Locals window. I need the ability to lazy load each base instance, so that when a “base” tree node is expanded, those properties and fields with values can be shown on demand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:40:08+00:00Added an answer on June 7, 2026 at 7:40 pm

    Here is a simple example which does what you specified. I’m sure you can modify this to your specific needs or at least it will point you in the right direction.

    You can copy and paste this example into a console application and set a few breakpoints to see how it works.

    The code in VB.Net

    Module Module1
    
        Private Class ClassOne
            Private _One As String
            Private _Two As Integer
            Private _three As Double
    
            Public Property One() As String
                Get
                    Return _One
                End Get
                Set(ByVal value As String)
                    _One = value
                End Set
            End Property
    
            Public Property Two() As Integer
                Get
                    Return _Two
                End Get
                Set(ByVal value As Integer)
                    _Two = value
                End Set
            End Property
    
            Public Property Three() As Double
                Get
                    Return _three
                End Get
                Set(ByVal value As Double)
                    _three = value
                End Set
            End Property
        End Class
    
        Private Class ClassAlpha
            Inherits ClassOne
    
            Private _Alpha As String
            Private _Beta As Long
    
            Public Property Alpha() As String
                Get
                    Return _Alpha
                End Get
                Set(ByVal value As String)
                    _Alpha = value
                End Set
            End Property
    
            Public Property Beta() As Long
                Get
                    Return _Beta
                End Get
                Set(ByVal value As Long)
                    _Beta = value
                End Set
            End Property
        End Class
    
        Private Class ClassColor
            Inherits ClassAlpha
    
            Private _Red As String
            Private _Blue As Long
    
            Public Property Red() As String
                Get
                    Return _Red
                End Get
                Set(ByVal value As String)
                    _Red = value
                End Set
            End Property
    
            Public Property Blue() As Long
                Get
                    Return _Blue
                End Get
                Set(ByVal value As Long)
                    _Blue = value
                End Set
            End Property
        End Class
    
        Sub Main()
            Dim o As New ClassColor()
            o.Red = "The Color Red"
            o.Blue = 14
            o.Alpha = "The First"
            o.Beta = 202
            o.One = "One"
            o.Two = 2
            o.Three = 3.1415927
    
            Dim helper As New ReflectionHelper(o)
    
            Dim list1 = helper.ReflectProperties(helper.BaseClasses(0), o)
            Dim list2 = helper.ReflectProperties(helper.BaseClasses(1), o)
            Dim list3 = helper.ReflectProperties(helper.BaseClasses(2), o)
        End Sub
    
    End Module
    
    Public Class ReflectionHelper
        Private _SourceClass As Object
        Private _BaseClasses As New List(Of Type)
    
        Public Sub New()
        End Sub
    
        Public Sub New(source As Object)
            _SourceClass = source
    
            Dim t As Type = _SourceClass.GetType()
            While (t IsNot Nothing)
                _BaseClasses.Add(t)
                t = t.BaseType
            End While
        End Sub
    
        Public ReadOnly Property BaseClasses As List(Of Type)
            Get
                Return _BaseClasses
            End Get
        End Property
    
        Public Function ReflectProperties(ByVal baseClass As Type,
                                          ByVal instance As Object) As List(Of ReflectionHelperProperties)
            Dim result As New List(Of ReflectionHelperProperties)
            For Each prop In baseClass.GetProperties(Reflection.BindingFlags.DeclaredOnly Or
                                                     Reflection.BindingFlags.GetProperty Or
                                                     Reflection.BindingFlags.Instance Or
                                                     Reflection.BindingFlags.Public)
                result.Add(New ReflectionHelperProperties() With {.Name = prop.Name, .InstanceValue = prop.GetValue(instance, Nothing)})
            Next
            Return result
        End Function
    End Class
    
    Public Class ReflectionHelperProperties
        Public Name As String
        Public InstanceValue As Object
    End Class
    

    The same code in C#

    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApplication23
    {
        class Program
        {
            static void Main(string[] args)
            {
                // create the highest level type
                ClassColor o = new ClassColor();
                o.Red = "The Color Red";
                o.Blue = 14;
                o.Alpha = "The First";
                o.Beta = 202;
                o.One = "One";
                o.Two = 2;
                o.Three = 3.1415927;
    
                ReflectionHelper helper = new ReflectionHelper(o);
    
                List<ReflectionHelperProperties> list1 = helper.ReflectProperties(helper.BaseClasses[0], o);
                List<ReflectionHelperProperties> list2 = helper.ReflectProperties(helper.BaseClasses[1], o);
                List<ReflectionHelperProperties> list3 = helper.ReflectProperties(helper.BaseClasses[2], o);
            }
        }
    }
    
    public class ClassOne
    {
        private string _One;
        private int _Two;
    
        private double _three;
        public string One {
            get { return _One; }
            set { _One = value; }
        }
    
        public int Two {
            get { return _Two; }
            set { _Two = value; }
        }
    
        public double Three {
            get { return _three; }
            set { _three = value; }
        }
    }
    
    public class ClassAlpha : ClassOne
    {    
        private string _Alpha;    
        private long _Beta;
    
        public string Alpha {
            get { return _Alpha; }
            set { _Alpha = value; }
        }
    
        public long Beta {
            get { return _Beta; }
            set { _Beta = value; }
        }
    }
    
    public class ClassColor : ClassAlpha
    {    
        private string _Red;    
        private long _Blue;
    
        public string Red {
            get { return _Red; }
            set { _Red = value; }
        }
    
        public long Blue {
            get { return _Blue; }
            set { _Blue = value; }
        }
    }
    
    public class ReflectionHelper
    {
        private List<Type> _BaseClasses = new List<Type>();
        public ReflectionHelper()
        {
        }
    
        public ReflectionHelper(object source)
        {
            // build base types list
            Type t = source.GetType();
            while ((t != null)) {
                _BaseClasses.Add(t);
                t = t.BaseType;
            }
        }
    
        public List<Type> BaseClasses {
            get { return _BaseClasses; }
        }
    
        public List<ReflectionHelperProperties> ReflectProperties(Type baseClass, object instance)
        {
            List<ReflectionHelperProperties> result = new List<ReflectionHelperProperties>();
            foreach (System.Reflection.PropertyInfo p in baseClass.GetProperties(System.Reflection.BindingFlags.DeclaredOnly | 
                                                                                 System.Reflection.BindingFlags.GetProperty | 
                                                                                 System.Reflection.BindingFlags.Instance | 
                                                                                 System.Reflection.BindingFlags.Public)) {
                result.Add(new ReflectionHelperProperties {
                    Name = p.Name,
                    InstanceValue = p.GetValue(instance, null)
                });
            }
            return result;
        }
    }
    
    public class ReflectionHelperProperties
    {
        public string Name;
        public object InstanceValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a base class that can be used to serve up
Ok, so here's a snippet of my Properties base class, that is extended in
I can use GetModuleHandle to get its base offset, but I also need to
We can get class Class object by 3 methods: MyClass.class obj.getClass Class.forName(className) I don't
I can get this to work: [<DllImport(user32.dll)>] extern bool GetClientRect(nativeint, RECT*) let getClientRect hwnd
You can get a list of databases using PRAGMA database_list or a list of
I wrote a class that allows a derivate to specify which of its properties
I know that you can get the username of the currently logged in user
I've written an abstract base class for unit tests that sets up just enough
Is there a base class that I should inherit from that is just a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.