1

I'm struggling in updating an object with the given key only.
right now i can update using this:

public class Person {
    public int Id {get;set;}
    public string Name {get;set;}
    public string Highest {get;set;}
    public ..... (so many more)
}

this is what im using.

var model = db.Person.Find(1);
model.Highest = "20 meters";

what i want for it is to behave like an array where i will only update the data based on key like:

 var key = "Highest";
 var model = db.Person.Find(1);
 model[key] = "20 meters";

so that i can use it as a function

public Person UpdateData(Person model,string key,string newvalue){
 model[key] = newvalue;
 return model;
}

im think of converting it to array first then update then convert back to object. but i cant find a way to convert it into array.

do you have any suggestion?

Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
Darvin
  • 21
  • 7
  • 1
    It is possible to do this using reflection API. But if you need to do this in a tight, performance heavy loop, it will suffer. Why do you need to do this in the first place? What happens if the new value is not string? – Hasan Emrah Süngü Apr 12 '21 at 10:20
  • What are you trying to do and why? You're working with objects, not dictionaries. These are properties, not keys. If you don't want to use strongly-typed objects, don't use an ORM. It would be easier to use a DataTable – Panagiotis Kanavos Apr 12 '21 at 10:24
  • Why are you trying to set properties by name? It matters – Panagiotis Kanavos Apr 12 '21 at 10:26

2 Answers2

1

I think you need to use the dynamic feature of C# to lookup properties by key, or through Reflection API similar to this answer C# dynamically set property.

But beware of type safety and runtime errors that could happen due to incompatible types.

Probably in your case the best bet is to use an Object Mapper library like automapper.

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70
  • i need in my other code the regular class, i think if i declare it as dynamic then it will be all the way dynamic, unless 1. i convert the regular class into dynamic. 2. update the data. 3. convert back to regular class. is this what you mean? if so, i dont know how to convert the class into dynamic. – Darvin Apr 12 '21 at 10:18
  • @Darvin why are you trying to set the properties by name in the first place? – Panagiotis Kanavos Apr 12 '21 at 10:25
1

You could change your UpdateData method so it uses reflection. I would drop the idea with the indexer (model[key]).

    public static Person UpdateData(Person model, string key, string newvalue)
    {
        model.GetType().GetProperty(key).SetValue(model, newvalue);
        return model;
    }

However, if you really want use indexer you can do something like this in your Person:

    public object this[string propName]
    {
        get => this.GetType().GetProperty(propName).GetValue(this);
        set => this.GetType().GetProperty(propName).SetValue(this, value);
    }

Then you could use it like this person["Highest"] = "45";, but I don't really recommend this approach.

Michal Rosenbaum
  • 1,801
  • 1
  • 10
  • 18
  • Thank you, i found this one and planning to close the thread, but yours is shorter, ` private void SetObjectProperty(string propertyName, string value, object obj) { PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName); // make sure object has the property we are after if (propertyInfo != null) { propertyInfo.SetValue(obj, value, null); } } ` from the link [link](https://stackoverflow.com/questions/12970353/c-sharp-dynamically-set-property) – Darvin Apr 12 '21 at 10:31