@ZEKE ROSS, Welcome to Microsoft Q&A, if you want to copy value from one object to another object without having to write out for each line, I recommend that you could use reflection to do it.
Here is a code example you could refer to.
internal class Program
{
static void Main(string[] args)
{
Example ex1 = new Example() { Name = "test1", Id = 1001, Address = "home" };
Example ex2 = new Example() { Name = "test2", Id = 1001, Address = "home2" };
Set<Example>(ex1, ex2);
}
private static void Set<T>(T to, T from)
{
var result1 = to.GetType().GetProperties().ToList();
var result2 = from.GetType().GetProperties().ToList();
for (int i = 0; i < result1.Count; i++)
{
if(result1[i].GetValue(to,null)!= result2[i].GetValue(from, null))
{
result1[i].SetValue(to, result2[i].GetValue(from, null));
}
}
}
}
public class Example
{
public string Name { get; set; }
public int Id { get; set; }
public string Address { get; set; }
}
As the following result shown. the ex1's property's value all set to be the same as the ex2's property's value.
Hope my solution could be helpful for you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.