If you want to keep things simple, you can simply create your new Type including all properties of given object and your desired new property, then fill your new class and do what do you want with it.
Also consider reading Note part.
For complicated cases and large applications, you can consider solutions like what abatishchev mentioned in his 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 it this way:
var foo = Service.GetFoo();
var fooViewModel= new FooViewModel(foo);
fooViewModel.NewProperty = "new value";
Note:
- You can use a
Foo
instance in FooViewModel
and all getters and setters act on that instance to keep thing synchronized.
- Sometimes you can enhance the solution using inheritance from
Foo
, this way you don't need too create each properties again.
- For complicated cases and large applications, you can consider solutions like what abatishchev mentioned in his answer.