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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:48:45+00:00 2026-06-03T22:48:45+00:00

I have an object with over 100 properties. I then have a list with

  • 0

I have an object with over 100 properties. I then have a list with a lot of these objects.
I need to calculate the Min, Max, Medium and Median on all the properties in the list.

So instead of writing:

Ag = _valuesForExtraCalculations.Min(c => c.Ag),
Al = _valuesForExtraCalculations.Min(c => c.Al),
Alkal = _valuesForExtraCalculations.Min(c => c.Alkal),

one hundred times, I thought I would be able to use reflection, so I wrote:

var Obj = new Analysis();
Type t = Obj.GetType();
PropertyInfo[] prop = t.GetProperties();
foreach (var propertyInfo in prop)
{
    propertyInfo = _valuesForExtraCalculations.Min(?????);
}

But I’m not sure what to write in the foreach loop, so I can set the new min value for that property to the new Analysis object I created.

  • 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-03T22:48:46+00:00Added an answer on June 3, 2026 at 10:48 pm

    You would need to know the exact type of the properties. Let’s assume it is int:

    var Obj = new Analysis();
    Type t = Obj.GetType();
    PropertyInfo[] prop = t.GetProperties();
    foreach (var pi in prop.Where(p => p.PropertyType == typeof(int)) 
    {
        int min = _valuesForExtraCalculations.Min(c => (int)pi.GetValue(c, null));
        propertyInfo.SetValue(Obj, min, null);
    }
    

    If you have different types that you want to calculate the minimum for, you won’t get around checking the types and switching accordingly so the correct overload of Min gets called.

    I would still not consider this the best or most performant solution.

    PropertyInfo.GetValue and PropertyInfo.SetValue are slower than direct field access (a lot slower according to this article), also it would involve a lot of boxing. You would call PropertyInfo.GetValue (countOfObjects * countOfProperties) times. Depending on the number of items and properties, that might be of concern.

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

Sidebar

Related Questions

I've got a list of objects (probably not more than 100), where each object
Suppose we have these business objects with some properties in common: public class A
I have a typical producer, consumer pattern. If the producer sends an object over
I have an object of type Hash that I want to loop over via
I have persistent object, with a string property that often is over 500 charachters.
I have an object in C# with lets say 20 properties and it's part
I need to have an instance of one common object in every other object,
I have over 100 different checkboxes in 6 activites and i would like to
I have object A which contains multiple instances of object B, which in turn
I have two tables: object that has object_id column and avalues that have object_id

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.