Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I deep copy an object of one object type to another object type that share an inheritance structure
    text
    copied!<p>I am currently building an object model for HL7 messages. Without diving into those, the basic structure that we have looks similar to this:</p> <ul> <li>Base Object <ul> <li>Intermediary Object <ul> <li>DeepMessage1</li> <li>DeepMessage2</li> </ul></li> <li>Message1</li> <li>Message2</li> </ul></li> </ul> <p>I want to have a deep copy/clone that will copy all of the similar properties from DeepMessage1 to DeepMessage2 or Message1 or Message2.</p> <pre><code>public class BaseObject { PersonName Name; //A personName includes a first name and last name. } public class IntermediaryObject : BaseObject { Accident accident; //An accident codes and a description. } public class Message1 : BaseObject { //this class doesn't actually contain any special handling, just used for //easy understanding for the API } public class Message2 : BaseObject { DateTime AdmissionDate; //Note the admission date is also contained in //DeepMessage1, but not DeepMessage2 } public class DeepMessage1 : IntermediaryObject { DateTime AdmissionDate; //Note this property is also contained in Message2 and //needs to be copied } public class DeepMessage2 : IntermediaryObject { DateTime DischargeDate; } </code></pre> <p>With that structure in mind, I want to be able to create a deep copy of every property that one object shares with another. <a href="https://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp">This other question</a> was a really good start, but ultimately I couldn't use reflection as that was shallow cloning, and serialization requires the exact same object.</p> <p>I ended up with this code, but it only performs a shallow copy.</p> <pre><code>public T Copy&lt;T&gt;() where T : new() { T destination = new T(); if (destination is HL7Message) { foreach (var destinationProperty in destination.GetType().GetProperties()) { foreach (var sourceProperty in this.GetType().GetProperties()) { if (destinationProperty.Name.Equals(sourceProperty.Name)) { destinationProperty.SetValue(destination, destinationProperty.GetValue(this, null), null); } } } return destination; } else { throw new InvalidOperationException("The destination copy type is not an HL7Message object"); } } </code></pre> <p>I was hoping that in my <code>if (destinationProperty.Name.Equals(sourceProperty.Name))</code> block I could attempt to call Copy on any properties that are of a specific base type (which all of the objects in my library extend). However, I couldn't get the Copy in that section to work as I can't determine the T at runtime.</p> <p>Do I just need to have a separate type of Copy for the specific objects and have the Messages use the Copy or is there a way to do this that is so damn crazy?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload