i have wcf web service consumed in mvc4 application returns object. want add property object after loaded. want clone of object plus new property.
how that?
would best deserialize json, add new property , serialize new object property or there way it?
if want keep things simple, can create new type including properties of given object , desired new property, fill new class , want it.
also consider reading note part.
for complicated cases , large applications, can consider solutions abatishchev mentioned in answer.
class foo { public int id { get; set; } public string name { get; set; } } class fooviewmodel { public fooviewmodel() { } public fooviewmodel(foo foo) { this.id= foo.id; this.name= foo.name; } public int id { get; set; } public string name { get; set; } public string newproperty{ get; set; } }
and use way:
var foo = service.getfoo(); var fooviewmodel= new fooviewmodel(foo); fooviewmodel.newproperty = "new value";
note:
- you can use
foo
instance infooviewmodel
, getters , setters act on instance keep thing synchronized. - sometimes can enhance solution using inheritance
foo
, way don't need create each properties again. - for complicated cases , large applications, can consider solutions abatishchev mentioned in answer.
Comments
Post a Comment