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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:35:20+00:00 2026-05-28T14:35:20+00:00

What I want to do is take any class type and create a list

  • 0

What I want to do is take any class type and create a list of ‘get’ accessors to all of the properties in the object graph.

The exact format, order, etc of the collection doesn’t matter, I just don’t quite know how to start off identifying and creating accessors to all of the properties. It might take the form of something like this:

public static List<Func<T,object>> CreateAccessors<T>()
{
   Type t = typeof(T);
   // Identify all properties and properties of properties (etc.) of T
   // Return list of lambda functions to access each one given an instance of T
}

public void MyTest()
{
   MyClass object1;
   var accessors = CreateAccessors<MyClass>();
   var myVal1 = accessors[0](object1);
   var myVal2 = accessors[1](object1);

   // myVal1 might now contain the value of object1.Property1
   // myVal2 might now contain the value of object1.Property4.ThirdValue.Alpha
}
  • 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-28T14:35:21+00:00Added an answer on May 28, 2026 at 2:35 pm

    You can use reflection to extract the properties and expression-trees to help build the delegates targeting the property getters:

                    // Start with all public instance properties of type
    var accessors = from property in type.GetProperties
                             (BindingFlags.Instance | BindingFlags.Public)
    
                    // Property must be readable
                    where property.CanRead
    
                    //Assemble expression tree of the form:
                    // foo => (object) foo.Property
    
                    // foo
                    let parameter = Expression.Parameter(type, "foo")
    
                    // foo.Property 
                    let propertyEx = Expression.Property(parameter, property)
    
                    // (object)foo.Property - We need this conversion
                    // because the property may be a value-type.
                    let body = Expression.Convert(propertyEx, typeof(object))
    
                    // foo => (object) foo.Property
                    let expr = Expression.Lambda<Func<T,object>>(body, parameter)
    
                    // Compile tree to Func<T,object> delegate
                    select expr.Compile();
    
    return accessors.ToList();
    

    Note that although Delegate.CreateDelegate seems like an obvious choice, you will have some problems boxing value-type properties. Expression-trees dodge this problem elegantly.

    Note that you’ll need some more work to be able to get “nested” properties out too, but hopefully I’ve given ypu enough to get you started (hint: recurse). One final pointer with that: watch out for cycles in the object graph!

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

Sidebar

Related Questions

I want to take an object, let's say this object: public class BenchmarkList {
I want to create a very generic utility method to take any Collection and
I want to take any real number, and return the closest number, with the
We want to take XML data and convert it to an NSDictionary object, but
Suppose I want to create a set of observers based on type. That is
The problem is that Type.GetInterfaces() returns all interfaces that a class implements, this includes
I want to take html, including the text and images and turn it into
I want to take the value stored in a 32 bit unsigned int, put
I want to take a floating point number in C++, like 2.25125, and a
I want to take a snapshot of a view (WebView) or, if that is

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.