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

The Archive Base Latest Questions

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

I was wondering if there is something in c# to be able to pass

  • 0

I was wondering if there is something in c# to be able to pass a member of a class to another function that will use this member to get a value. So get a value of a field determined only which one at runtime. Something like in other languages (PHP at least I think) that you can do

a.b = "something"

but also

a["b"] = "something";

edit: actually not so good an example since a string is used, sorry

For clarity an example of what I’d like to be able to do:

class A
{
    int x;
    int y;
}

void somethingsomething<T>(T class, SomeMagicFieldClass f)
{
    dosomethingwith(somemethodthatgivesmethevalueoffield(class, f));
}

Where then I can call the method like this:

A a = new A();
somethingsomething(a, A.x); //hypothetical notation
somethingsomething(a, A.y);

I now have something similar where I do:

somethingsomething(a, "x");
somethingsomething(a, "y");

I then go find the field using introspection API (also trying GetProperty)

MemberInfo memberInfo = item.GetType().GetField(fieldName);

This works but the disadvantage is that the fields passed as a string won’t get updated when “refactoring” fieldnames in visual studio, so I was thinking maybe there exists something like this in c# that would get refactored automatically when changing field names?

Thanks a lot for reading this boring question

  • 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-20T14:57:09+00:00Added an answer on May 20, 2026 at 2:57 pm

    You can do some nice refactor-friendly things with LINQ Expressions. Here is a snippet of utilty code I used for such occasions. It allows you to get the Name, Type and Value of a property (it won’t work with fields without modifications). There’s also a setter for the value.

    public static void Main(string[] args) {
        var test = new { Test1 = 42, Test2 = "123", Test3 = 3.14195 };
    
        somethingSomething(test, t => t.Test1);
        somethingSomething(test, t => t.Test2);
        somethingSomething(test, t => t.Test3);
    }
    
    static void somethingSomething<TObj,TProperty>(TObj obj, Expression<Func<TObj,TProperty>> expr) {
        var accessor = GetMemberAccessor(expr, obj);
    
        String name = accessor.Name;
        TProperty value = accessor.Value;
        String typeName = accessor.Type.Name;
        Console.WriteLine("{0} = {1} ({2})", name, value, typeName);
    }
    

    The output of that would be:

    Test1 = 42 (Int32)
    Test2 = 123 (String)
    Test3 = 3.14195 (Double)
    

    To make this work, I used the following helper function and class:

    public static MemberAccessor<TReturn> GetMemberAccessor<TObj,TReturn>(Expression<Func<TObj, TReturn>> expr, TObj tar) {
        var body = expr.Body;
    
        MemberExpression memberExpression = null;
        if (body is UnaryExpression) {
            var ue = (UnaryExpression)body;
            memberExpression = (MemberExpression)ue.Operand;
        } else if (body is MemberExpression)
            memberExpression = (MemberExpression)body;
        else
            throw new NotImplementedException("can't get MemberExpression");
    
        String name = memberExpression.Member.Name;
    
        return new MemberAccessor<TReturn>(tar, name);
    }
    
    public class MemberAccessor<T> {
        private readonly PropertyDescriptor propertyDesc;
        private readonly Object target;
    
        public MemberAccessor(Object target, String propertyName) {
            this.target = target;
            this.propertyDesc = TypeDescriptor.GetProperties(target)[propertyName];
        }
        public String Name {
            get { return propertyDesc.Name; }
        }
        public Type Type {
            get { return propertyDesc.PropertyType; }
        }        
        public T Value {
            get { return (T)Convert.ChangeType(propertyDesc.GetValue(target), typeof(T)); }
            set { propertyDesc.SetValue(target, value); }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Came across something like this today, and was wondering if there was an equivalent
Wondering if there is any way to get the lambda expressions that result from
This is something I used to do in Java, I was wondering if there
I am wondering if there is something already like this out there. But I
Just wondering, if I want to create a class that does something and I
In PHP there's a function called stream_wrapper_register . With that i can get the
I'm wondering if there is something like Hotswap/HotDelpoy/JRebel (known from Java World) in .NET
Wondering if there is a good way to generate temporary URLs that expire in
Wondering if there is any tool that can help me to detect a pronoun's
Just wondering if there is any way to get the NS records in C#.

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.