samedi 14 novembre 2015

Easiest way to dynamically get/set properties of c# objects by name

I have several objects of different types and the name of a property that I need to get and set on these. In languages like javascript this is trivial as I can get and set values with indexor syntax.

obj2[propertyName] = obj1[propertyName];

In c# it's not so easy. I know I can use reflection but that's a lot of code. I know there's dynamic objects, but they don't automatically have a way to reference properties by name. What's the easiest way to get and set properties dynamically by name on objects of arbitrary types (ie they have the property but don't all share a common interface or base class)?





Aucun commentaire:

Enregistrer un commentaire