I have
public class MyClass1
{
public string Name;
public string Address;
}
public class MyClass2
{
public int Id;
public string Name;
public string Address;
}
var a = new MyClass1 {Name="SomeName", Address="SomeAddress" };
I dont want to do
b.Name = a.Name;
b.Address = a.Address
because I have more than 30 fields.
I want is:
MyClass2 b = a;
What you want is not possible. I would recommend you AutoMapper which allows you to define a mapping:
and then map between instances of those two classes:
As a bonus you will be able to do the following:
Another possibility is to use reflection to loop through all properties of the source object and set them in the target object but why reinventing wheels when such great tools like AutoMapper exist?