-1

I have a web service that returns a custom object (a struct type) APIStruct user. I have a variable holding the name of the current field it is checking for currentField.

Now, in a situation where user contains first_name and last_name, I can access the value using user.first_name or user.last_name. BUT, is it possible to hold the field name in a variable and access the value through the variable? working like:

var currentField = "first_name";
var value = user.currentField;

Obviously the above is not working, so is there any way to do this? In the past with languages such as PowerShell it works just like above $currentField = "first_name"; $value = user.$currentField

I've tried user.currentField user.(currentField) user[currentField] user.$currentField

Seth
  • 972
  • 1
  • 7
  • 18
  • 4
    Yes this is possible. Please do a little research before asking a question that's been asked by many others :) http://stackoverflow.com/questions/10283206/c-sharp-setting-getting-the-class-properties-by-string-name – Jonathan Carroll Mar 02 '16 at 23:40
  • I did do research and couldn't find the answer. Just because the question exist somewhere here doesn't mean I used the exact terminology they did to come across it in my search. I searched what I could and nothing came back properly answering my question. So next time, please offer helpful advice or a simple link to the other questing and mark as duplicate instead of criticism and shaming with assumptions that no research was done. – Seth Mar 03 '16 at 14:55
  • I did provide a link to the answer in question. I literally searched "how to access class property by string value" and it was the first result. I wasn't shaming or criticizing either, I'm sorry you took it that way. – Jonathan Carroll Mar 03 '16 at 16:42
  • You can also copy and paste the title to your question into google and see that the first result is also an answer to your question ;) – Jonathan Carroll Mar 03 '16 at 16:51

4 Answers4

1

You can extend your object class to support access to a Dictionary of additional properties, accessible through an explicit indexer.

public class myClass
{
    private Dictionary<string, object> Something = new Dictionary<string, object>();
    public object this[string i]
    {
        get { return Something[i]; }
        set { Something[i] = value; }
    }
}

Use like this:

    myClass m = new myClass();

Set value:

    m["fist name"] = "Name";
    m["level"] = 2;
    m["birthday"] = new DateTime(2015, 1, 1);

Get value:

    int level = (int)m["level"];
    string firstName = (string)m["first name"];
    DateTime dt = (DateTime)m["birthday"];
Community
  • 1
  • 1
NoName
  • 7,940
  • 13
  • 56
  • 108
0

What you're looking for called Reflection.

var type = user.GetType(); // Get type object
var property = type.GetProperty(currentField); // get property info object
var value = property.GetValue(user); // get value from object.

Be careful - reflection is pretty slow compared to direct property access.

Oleh Nechytailo
  • 2,155
  • 17
  • 26
0

you must use reflection. Create a method like this:

public static object GetPropValue(object src, string propName)
{
    return src.GetType().GetProperty(propName).GetValue(src, null);
}

and call it:

string currentField = "first_name";
GetPropValue(user, currentField);

But it must be said, it is not the way you should use for the standard reading of object values.

o..o
  • 1,789
  • 5
  • 31
  • 59
-2

Another option you may have is to use a switch statement. Something like:

switch (currentField){
    case "first_name":
            value = user.first_name;
            break;
    case "last_name":
            value = user.last_name;
            break;

etc...