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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:20:22+00:00 2026-06-15T10:20:22+00:00

I have a function that filters objects of a specific class by a chosen

  • 0

I have a function that filters objects of a specific class by a chosen field. The way I currently do this is that I pass a string naming the field as an argument to the function. Ideally I would want to be able to use this string to select the field in the object, similar to a dictionary (this functionality exists in javascript for example).

So I have the function (cut down to the relevant bits) here:

private List<HangingArtBundle> ConstrainBundlesBy(List<HangingArtBundle> bundles, string valueString, string constraint)
{
    List<HangingArtBundle> retBundles = new List<HangingArtBundle>();
    List<string> values = new List<string>(valueString.Split(new char[] { '|' }));

    foreach (HangingArtBundle bundle in bundles)
    {
        if (values.Contains(ConstrainedField(constraint, bundle)))
        {
            retBundles.Add(bundle);
        }
    }

    return retBundles;
}

I would like to be able to replace the ConstrainedField(constraint, bundle) part with something like bundle[constraint], where constraint is the name of a field of the class HangingArtBundle. Instead, I have to use this function below, which requires me to manually add field names as needed:

private string ConstrainedField(string field, HangingArtBundle bundle)
{
    if (field.Equals("Category"))
    {
        return bundle.Category;
    }
    else if (field.Equals("AUToolTab"))
    {
        return bundle.AUToolTab;
    }
    else
    {
        return "";
    }
}

If it helps at all, here is the class (essentially just a struct):

public class HangingArtBundle
{
    public string CP { get; set; }
    public string Title { get; set; }
    public string Category { get; set; }
    public string AUToolTab { get; set; }
    public List<HangingArtEntry> Art { get; set; }
}

Is this possible to do in an elegant way in C#?

  • 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-15T10:20:24+00:00Added an answer on June 15, 2026 at 10:20 am

    You can use System.Reflection for this

     private string GetField(HangingArtBundle hab, string property)
     {
        return (string)hab.GetType().GetProperty(property).GetValue(hab, null);
     }
    

    enter image description here

    Or perhaps an Extension method to make life a bit easier:

        public static class Extensions
        {
            public static string GetField(this HangingArtBundle hab, string property)
            {
                if (hab.GetType().GetProperties().Any(p => p.Name.Equals(property)))
                {
                    return (string)hab.GetType().GetProperty(property).GetValue(hab, null);
                }
                return string.Empty;
            }
        }
    

    Usage:

      string result = bundle.GetField("CP");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that takes an input string and then runs the string
I have a GeoDjango project that has a manager model like this; class AdvertManager(models.GeoManager):
Using mootools I have a 'builder' class that manufactures form objects, dynamically creating divs
I have been fruitlessly trying for several hours to make a function that filter
I have a control that has a Filter property that expects a function that
Inside my Controller i have function that runs after user clicks on item, which
So I have function that formats a date to coerce to given enum DateType{CURRENT,
In my Java code I have function that gets file from the client in
I have function Start() that is fired on ready. When I click on .ExampleClick
I have a function that returns two values in a list. Both values need

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.