What you want is impossible. I would recommend you AutoMapper , which allows you to define a mapping:
Mapper.CreateMap<MyClass1, MyClass2>();
and then map between instances of these two classes:
var a = new MyClass1 { Name = "SomeName", Address = "SomeAddress" };
var b = Mapper.Map<MyClass1, MyClass2>(a);
As a bonus, you can do the following:
IEnumerable<MyClass1> list1 = ...
IEnumerable<MyClass2> list2 = Mapper.Map<IEnumerable<MyClass1>, IEnumerable<MyClass2>>(list1);
Another possibility is to use reflection to scroll through all the properties of the source object and set them in the target object, but why reinvent the wheels when there are such large tools as AutoMapper?
source
share