Klompenrunner
Klompenrunner

Reputation: 783

C# Class Object JavaScript like use

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";

Upvotes: 0

Views: 360

Answers (3)

Christian Gollhardt
Christian Gollhardt

Reputation: 17024

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.

Upvotes: 2

TSungur
TSungur

Reputation: 406

You can use Reflection in C# like this:

var prop=row.GetType().GetProperty("id");
prop.SetValue(row,"id123");

Upvotes: 2

dannybucks
dannybucks

Reputation: 2355

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.

Upvotes: 1

Related Questions