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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:57:59+00:00 2026-05-16T00:57:59+00:00

I want to build a method which accepts a string param, and an object

  • 0

I want to build a method which accepts a string param, and an object which I would like to return a particular member of based on the param. So, the easiest method is to build a switch statement:

public GetMemberByName(MyObject myobj, string name)
{
   switch(name){
     case "PropOne": return myobj.prop1;
     case "PropTwo": return myobj.prop2; 
   }
}

This works fine, but I may wind up with a rather large list… So I was curious if there’s a way, without writing a bunch of nested if-else structures, to accomplish this in an indexed way, so that the matching field is found by index instead of falling through a switch until a match is found.

I considered using a Dictionary<string, something> to give fast access to the matching strings (as the key member) but since I’m wanting to access a member of a passed-in object, I’m not sure how this could be accomplished.

  • I’m specifically trying to avoid reflection etc in order to have a very fast implementation. I’ll likely use code generation, so the solution doesn’t need to be small/tight etc.

  • I originally was building a dictionary of but each object was initializing it. So I began to move this to a single method that can look up the values based on the keys- a switch statement. But since I’m no longer indexed, I’m afraid the continuous lookups calling this method would be slow.

  • SO: I am looking for a way to combine the performance of an indexed/hashed lookup (like the Dictionary uses) with returning particular properties of a passed-in object. I’ll likely put this in a static method within each class it is used for.

  • 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-16T00:58:00+00:00Added an answer on May 16, 2026 at 12:58 am

    Here’s an easy way you can use a dictionary:

        Dictionary<string, Func<MyObject, object>> propertyNameAssociations;
    
        private void BuildPropertyNameAssociations()
        {
            propertyNameAssociations = new Dictionary<string, Func<MyObject, object>>();
            propertyNameAssociations.Add("PropOne", x => x.prop1);
            propertyNameAssociations.Add("PropTwo", x => x.prop2);
        }
    
        public object GetMemberByName(MyObject myobj, string name)
        {
            if (propertyNameAssociations.Contains(name))
                return propertyNameAssociations[name](myobj);
            else
                return null;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 540k
  • Answers 540k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One expectation per group is a suggestion, not a hard… May 17, 2026 at 2:35 am
  • Editorial Team
    Editorial Team added an answer You can create the SQL script from a database using… May 17, 2026 at 2:35 am
  • Editorial Team
    Editorial Team added an answer I frequently have more than one assertion. If it's all… May 17, 2026 at 2:35 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want to build my own custom view which should look like the Crysis-GUI.
I have method which gets a POJO as it's parameter. Now I want to
I'm trying build a method which returns the shortest path from one node to
I need to write a server which accepts connections from multiple client machines, maintains
I want to build a SOAP client using ruby. I tried using the soap4r
I want to share some information between two classes (A and B), which are
We want to build a report in Microsoft Dynamics AX 2009 to show all
UPDATE I reflected Microsoft.Cci.dll and build my rule. It works fine. However, I am
Wondering if there is any way to build and fire an event (e.g. on
I'm struggling to work out how best to do what I think I want

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.