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

  • SEARCH
  • Home
  • 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 9155543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:36:58+00:00 2026-06-17T12:36:58+00:00

I am attempting to cycle through the Fields in my class, place the fields

  • 0

I am attempting to cycle through the Fields in my class, place the fields of a certain type into a list and then return that list. I know I am missing some key component but I cannot figure out what I am doing wrong. This is what I have so far:

...

I need to return a list of the actual fields as pointers, not a copy of the data. Any help in getting this to work would be appreciated.

I removed the above code (you should still be able to see it in the history) because it was confusing. This is the updated code (with some extra stuff removed) that works thanks to the answer from competent_tech:

string prop1;
BaseClass prop2;
SubClass prop3;
SubClass prop4;

public List<BaseClass> GetData()
{
    List<BaseClass> DataList = new List<BaseClass>();

    foreach (System.Reflection.PropertyInfo thisInfo in this.GetType().GetProperties())
    {
        var tempPropery = thisInfo.GetValue(this, null);
        if (tempPropery.GetType().IsSubclassOf(typeof(BaseClass)) || tempPropery.GetType().Equals(typeof(BaseClass)))
        {
            DataList.Add((BaseClass)tempPropery);
        };
    }

    return DataList;
}

The above code will allow you to get all the properties of a specific base type from your class and return them in a list. So the above code would return prop2 – prop4.

  • 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-17T12:36:58+00:00Added an answer on June 17, 2026 at 12:36 pm

    You can accomplish this through reflection.

    foreach (System.Reflection.PropertyInfo oProperty in this.GetType().GetProperties()) {
    }
    

    There are numerous ways that you can use this information once you have it or filter the properties (for example, by adding an attribute to the properties that you want to collect).

    You can get more information from MSDN.

    Note that this code specifically references properties, but there are equivalent methods for retrieving fields (GetFields) and all members (GetMembers).

    Once you have the PropertyInfo, you can call the GetValue method:

            foreach (System.Reflection.PropertyInfo oProperty in this.GetType().GetProperties()) {
                Console.WriteLine(oProperty.Name + " = " + oProperty.GetValue(this, null).ToString());
            }
    

    Alternatively, if you are looking at both fields and properties:

            foreach (System.Reflection.MemberInfo oMember in this.GetType().GetMembers())
            {
                switch (oMember.MemberType)
                {
                    case System.Reflection.MemberTypes.Field:
                        var oField = (System.Reflection.FieldInfo)oMember;
                        Console.WriteLine(oField.Name + " = " + oField.GetValue(this).ToString());
                        break;
    
                    case System.Reflection.MemberTypes.Property:
                        var oProperty = (System.Reflection.PropertyInfo)oMember;
                        Console.WriteLine(oProperty.Name + " = " + oProperty.GetValue(this, null).ToString());
                        break;
    
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to deserialize a stream to List<T> (or any other type) and am failing
At this url - http://thespacebetweenthewords.org/sandbox/ I am attempting to cycle through arrays of words
I am attempting to create a slot machine type animation in which a list
Attempting to apply .hover to a class that shows a div based on a
I am attempting to cycle four images that have slightly more brightness, I'm attempting
I am attempting to take a list of numbers 0-9 inclusive and return all
Attempting to print out a list of values from 2 different variables that are
Attempting to make a NSObject called 'Person' that will hold the login details for
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng
Attempting to move an uploaded file so that it is saved in the directory,

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.