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 3324330
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:24:31+00:00 2026-05-17T23:24:31+00:00

I access property value from a class object at run-time using reflection in C#.

  • 0

I access property value from a class object at run-time using reflection in C#.

    public bool GetValue(string fieldName, out object fieldValue)
    {
        // Get type of current record
        Type curentRecordType = _currentObject.GetType();
        PropertyInfo property = curentRecordType.GetProperty(fieldName);

        if (property != null)
        {
            fieldValue = property.GetValue(_currentObject, null).ToString();
            return true;
        }
        else
        {
            fieldValue = null;
            return false;
        }
    }

I pass Property Name as parameter: fieldName to this method.
Now, I need to access a property value from the child object of above class at run-time.
Can anyone there please guide how can I access child object property value?

  • 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-05-17T23:24:32+00:00Added an answer on May 17, 2026 at 11:24 pm

    Since you want to be able to find objects on arbitrarily-nested child objects, you need a function that you can call recursively. This is complicated by the fact that you may have children that refer back to their parent, so you need to keep track of which objects you’ve seen before in your search.

    static bool GetValue(object currentObject, string propName, out object value)
    {
        // call helper function that keeps track of which objects we've seen before
        return GetValue(currentObject, propName, out value, new HashSet<object>());
    }
    
    static bool GetValue(object currentObject, string propName, out object value,
                         HashSet<object> searchedObjects)
    {
        PropertyInfo propInfo = currentObject.GetType().GetProperty(propName);
        if (propInfo != null)
        {
            value = propInfo.GetValue(currentObject, null);
            return true;
        }
        // search child properties
        foreach (PropertyInfo propInfo2 in currentObject.GetType().GetProperties())
        {   // ignore indexed properties
            if (propInfo2.GetIndexParameters().Length == 0)
            {
                object newObject = propInfo2.GetValue(currentObject, null);
                if (newObject != null && searchedObjects.Add(newObject) &&
                    GetValue(newObject, propName, out value, searchedObjects))
                    return true;
            }
        }
        // property not found here
        value = null;
        return false;
    }
    

    If you know what child object your property is in you can just pass the path to it, like so:

    public bool GetValue(string pathName, out object fieldValue) 
    { 
        object currentObject = _currentObject;
        string[] fieldNames = pathName.Split(".");
    
        foreach (string fieldName in fieldNames)
        {
            // Get type of current record 
            Type curentRecordType = currentObject.GetType(); 
            PropertyInfo property = curentRecordType.GetProperty(fieldName); 
    
            if (property != null) 
            { 
                currentObject = property.GetValue(currentObject, null).ToString(); 
            } 
            else 
            { 
                fieldValue = null; 
                return false; 
            } 
        }
        fieldValue = currentObject;
        return true; 
    } 
    

    Instead of calling it like GetValue("foo", out val) you would call it like GetValue("foo.bar", out val).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to access a property value of AppDelegate class from someView Controller without creating
every time i create a FileInfo object and access it's lastaccesstime property, it's always
If we make any class in VB.NET and try to access its property in
Visual Basic allows for properties with mixed access levels, for example Public Property Name()
I want to load the key value pairs from a class, or id, in
I have a static class which I use to get access to my public
In C#, what is the best way to access a property of the derived
I need to access the FOV property of the Perspective viewport. This is not
This time I have problem with virtual fields. I have core class for my
I have a query in Data Access Object DAOComments that joins users table and

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.