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.
Dictionary<string, whatever>for example..Nameand.PropertyTypeof the definition in a foreach loop. Very open to other ways; just stuck with something simple here...