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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:34:21+00:00 2026-06-14T15:34:21+00:00

A class: public class Person { public string Title; public string Name; public Int32

  • 0

A class:

public class Person
{
    public string Title;
    public string Name;
    public Int32 Age;
}

I have a list of strings

List<String> fields = new List<String>()
{
    "Title",
    "Age"
};

I would now like to, given the above list of strings, WriteLine the listed fields while iterating through a list of Person objects.

var persons = new List<Person>();

//Populate persons

foreach(Person person in persons)
{
    //Print out Title and Age of every person (because Title and Age are listed in fields)
}

What I’ve Tried:

  • What I’ve tried works, but it seems extremely inefficient. I create a Dictionary<String, object> for every iteration and assign every field of the object to an entry in the dictionary and then only evaluate the dictionary entries by matching the keys to the items in the fields list.
  • 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-14T15:34:22+00:00Added an answer on June 14, 2026 at 3:34 pm

    Strange requirement, you need inefficient reflection, for example:

    IEnumerable<PropertyInfo> properties = typeof(Person)
                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => fields.Contains(p.Name));
    
    foreach (Person person in persons)
    {
        foreach (PropertyInfo prop in properties)
            Console.WriteLine("{0}: {1}", prop.Name, prop.GetValue(person, null));
    }
    

    DEMO

    I’ve only just seen that you’re probably looking for fields instead of properties. Then use this similar code:

    IEnumerable<FieldInfo> fields = typeof(Person)
        .GetFields( BindingFlags.Public | BindingFlags.Instance)
        .Where(f => fieldNames.Contains(f.Name)); // fieldNames is your List<string>
    
    foreach (Person person in persons)
    {
        foreach (FieldInfo field in fields)
            Console.WriteLine("{0}: {1}", field.Name, field.GetValue(person));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following list: class Person { public String Name { get; set;
I have an entity like this: public class Person { public string Name {
I have a custom class: public class Person { public String Name { get;
I have the following code: class Person { public String Name { get; set;
class Person { string name; public Person(string name) { this.name = name; } public
Consider this simple example - public class Person { private String name; private Date
So I have the following model: public class Person { public String FirstName {
Let's say we have a collection of Person objects class Person { public string
I have an object that has a child collection: class Person { public string
Example-I have a person class Public Class Person Private _fname As String Public Property

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.