0

In C# I have a class object defined like:

public class Row
    {
        public string id { get; set; }
        public string full_name { get; set; }
        public string email { get; set; }
    } 

Next I can use it like:

Row row = new Row();

And then do something like this to set a value:

row.id = "id123";

How do I make some type of "dynamic" reference? This doesn't work:

string col = "id";  
row[col] = "id123";
2
  • 1
    by using a Dictionary<string, whatever> for example. Commented Apr 3, 2019 at 11:55
  • @RoadRunner: This is just a snippet. This is part of a much larger class library where I need to traverse over many SQL table columns of many tables. If I already have columns defined in my object I can use the .Name and .PropertyType of the definition in a foreach loop. Very open to other ways; just stuck with something simple here... Commented Apr 3, 2019 at 12:12

3 Answers 3

2

You can use Reflection in C# like this:

var prop=row.GetType().GetProperty("id");
prop.SetValue(row,"id123");
Sign up to request clarification or add additional context in comments.

Comments

2

To answer your exact question, you could create a custom indexer:

public object this[string key]
{
    get
    {
        switch(key)
        {
             case nameof(id): return id;
             case nameof(full_name): return full_name;
             case nameof(email): return email;
             default: throw new ArgumentOutOfRangeException();
        }
    }      
    set
    {
        switch(key)
        {
             case nameof(id):
                 id = value.ToString();
                 break;
             case nameof(full_name):
                 full_name = value.ToString();
                 break;
             case nameof(email):
                 email = value.ToString();
                 break;
             default: throw new ArgumentOutOfRangeException();
        }
    }
}

public void Foo()
{
    var row = new Row();
    row["id"] = "Foo";
}

Or you use reflection as TSungur has answered:

public object this[string key]
{
    get
    {
        var prop = GetType().GetProperty(key);
        return prop.GetValue(this);
    }      
    set
    {
        var prop = GetType().GetProperty(key);
        prop.SetValue(this, value);
    }
}

However, If I were you, I would review your current library design. Probably you want to use an ORM like Entity Framework, which does all the mapping for you.

Comments

1

C# is a strongly typed language. This means that once a type is defined, you can't changed it dynamically during run time*. You also can't access the properties of an object with [] like in JavaScript. Therefore you can't achieve what you are looking for in C#. C# way would most likely be to access the id property directly through row.id = "id23";. In C# you always know during compile time what properties and methods are available on an object. If you need more flexibility what properties will be there, you can also use a Dictionary, KeyValuePair or simply a List.

*There is actually a dynamic key word that gives you some of that functionality - but it's uncommon to use that all over the place. Coming from JavaScript I would recommend to forget about it for the moment. There is almost always an other, "more C#-like" way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.