jeudi 29 septembre 2016

c# object initialization - possible to reference properties by non-literal name?

Trying to write a data-driven data mapping utility. But I need to be able to initialize objects (rows in a list) referencing the property (column) names as variables.

So for example instead of this:

var p = new person {name = "Joe", age = 10}

can I do something like...

string prop1 = "name", prop2 = "age";
var p = new person {[prop1] = "Joe", [prop2] = 10}

...or, could we treat the properties as an ordered collection somehow, like

var p = new person {prop[1] = "Joe", prop[2] = 10}

...or implicitly order them by simply dropping the property names, assuming the properties would be implied...

var p = new person {"Joe", 10}





Aucun commentaire:

Enregistrer un commentaire