Skip to main content

Posts

Showing posts from July, 2013

ASP.NET MVC - Apply properties values from one object to another of the same type automatically?

  have a type in   MiscUtil   called   PropertyCopy   which does something similar - although it creates a new instance of the target type and copies the properties into that. It would be easy to extend it to do the same thing with an existing instance though. It doesn't require the types to be the same - it just copies all the readable properties from the "source" type to the "target" type. Of course if the types are the same, that's more likely to work :) It's a shallow copy, btw. Let me know if you'd be interested in me extending the class for you. The syntax would be something like: MyType instance1 = new MyType (); // Do stuff MyType instance2 = new MyType (); // Do stuff PropertyCopy . Copy ( instance1 , instance2 ); (where   Copy   is a generic method called using type inference). EDIT: Okay, I've extended the capabilities of the class. To copy from one instance to another, it uses simple   PropertyInfo   values...