Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to solve this problem, I ended up with a 2-tier approach. I am not making any assertions that the map mentioned by devman could not have worked, but with limited time I was not able to explore his solution.</p> <p>First, all of my objects extended the same base object (BaseLibraryClass for the example). I originally did this because every class was going to original inherit INotifyPropertyChanged. To handle raising the property changed, I created a base object all of my objects inherited from. On this base class, I also added an abstract DeepCopy() method. Every object would override the DeepCopy() method by creating a new instance of itself and assigning the values over or, in the case of custom properties, call DeepCopy() on those properties.</p> <pre><code>Class Cat : BaseLibraryClass { public string Name; public Collar MyCollar; public override object DeepCopy() { Cat destination = new Cat(); Cat.Name = Name; Cat.MyCollar = MyCollar.DeepCopy(); } } Class Collar { ... } </code></pre> <p>Now I combined this with a modified version of the reflection loop in my original question. All of the objects that I wanted to be able to do this sort of Copy inherited the same base class. In the example case, an BaseCopyObject.</p> <pre><code>public T CopyObject&lt;T&gt;() where T : new() { T destination = new T(); if (destination is BaseCopyObject) { foreach (var destinationProperty in destination.GetType().GetProperties()) //iterate through the properties of the destination object. { foreach (var sourceProperty in this.GetType().GetProperties()) // iterate through the properties of the source object to determine if the property names match. If they do, copy the value. { //If we have one of our BaseCopyObjects, then set the value with the DeepCopy() method of that object. if (destinationProperty.Name.Equals(sourceProperty.Name)) { if (typeof(BaseLibraryClass).IsAssignableFrom(destinationProperty.PropertyType)) { BaseCopyObject var = (BaseLibraryClass)sourceProperty.GetValue(this, null); if (var != null) { destinationProperty.SetValue(destination, var.DeepCopy(), null); } break; } //If we have a list, get the list and iterate through the list objects. else if (typeof(IList).IsAssignableFrom(destinationProperty.PropertyType)) { IList destinationList = (IList)destinationProperty.GetValue(destination, null); destinationList.Clear(); IList sourcelist = (IList)destinationProperty.GetValue(this, null); if (sourcelist != null) { foreach (object listItem in sourcelist) { if (listItem is Base) { BaseLibraryClass n = (BaseLibraryClass)listItem; destinationList.Add(n); } } } break; } //Finally we get to normal properties. Set those. else { destinationProperty.SetValue(destination, destinationProperty.GetValue(this, null), null); break; } } } } return destination; } else { throw new InvalidOperationException("The destination copy type is not an BaseLibraryClass object"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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