Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Finally, I implemented the original requirement by my own although I didn't need it anyways (requirement changed). I'll provide the code here in case somebody needs it (and more or less as proof of concept, as a lot of improvement can still be done) or is interested in in, bear in mind, that the XML is a error prone, crucial component in this approach, property names and types have to match exactly, otherwise it wont work, but with a little GUI to edit the file this should be achieveable (I mean not editing the file manually).</p> <p>I used code from <a href="https://stackoverflow.com/questions/12294242/how-to-set-vaues-to-the-nested-property-using-c-sharp-reflection">here</a> and <a href="https://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp">here</a> and added class PropertyMapping to store the mappings read from the XML and also classes Foo and Bar to create a nested data structure, to copy to.</p> <p>Anyways here is the code, maybe it helps somebody some time:</p> <p>Main:</p> <pre><code>public class Program { public static void Main(string[] args) { // Model var calendarEvent = new CalendarEvent { EventDate = new DateTime(2008, 12, 15, 20, 30, 0), Title = "Company Holiday Party" }; MyObjectMapper mTut = new MyObjectMapper(@"SampleMappings.xml"); Console.WriteLine(string.Format("Result MyMapper: {0}", Program.CompareObjects(calendarEvent, mTut.TestMyObjectMapperProjection(calendarEvent)))); Console.ReadLine(); } public static bool CompareObjects(CalendarEvent calendarEvent, CalendarEventForm form) { return calendarEvent.EventDate.Date.Equals(form.EventDate) &amp;&amp; calendarEvent.EventDate.Hour.Equals(form.EventHour) &amp;&amp; calendarEvent.EventDate.Minute.Equals(form.EventMinute) &amp;&amp; calendarEvent.Title.Equals(form.Title); } } </code></pre> <p>Mapper implementation:</p> <pre><code>public class MyObjectMapper { private List&lt;PropertyMapping&gt; myMappings = new List&lt;PropertyMapping&gt;(); public MyObjectMapper(string xmlFile) { this.myMappings = GenerateMappingObjectsFromXml(xmlFile); } /* * Actual mapping; iterate over internal mappings and copy each source value to destination value (types have to be the same) */ public CalendarEventForm TestMyObjectMapperProjection(CalendarEvent calendarEvent) { CalendarEventForm calendarEventForm = new CalendarEventForm(); foreach (PropertyMapping propertyMapping in myMappings) { object originalValue = GetPropValue(calendarEvent,propertyMapping.FromPropertyName); SetPropValue(propertyMapping.ToPropertyName, calendarEventForm, originalValue); } return calendarEventForm; } /* * Get the property value from the source object */ private object GetPropValue(object obj, String compoundProperty) { foreach (String part in compoundProperty.Split('.')) { if (obj == null) { return null; } Type type = obj.GetType(); PropertyInfo info = type.GetProperty(part); if (info == null) { return null; } obj = info.GetValue(obj, null); } return obj; } /* * Set property in the destination object, create new empty objects if needed in case of nested structure */ public void SetPropValue(string compoundProperty, object target, object value) { string[] bits = compoundProperty.Split('.'); for (int i = 0; i &lt; bits.Length - 1; i++) { PropertyInfo propertyToGet = target.GetType().GetProperty(bits[i]); propertyToGet.SetValue(target, Activator.CreateInstance(propertyToGet.PropertyType)); target = propertyToGet.GetValue(target, null); } PropertyInfo propertyToSet = target.GetType().GetProperty(bits.Last()); propertyToSet.SetValue(target, value, null); } /* * Read XML file from the provided file path an create internal mapping objects */ private List&lt;PropertyMapping&gt; GenerateMappingObjectsFromXml(string xmlFile) { XElement definedMappings = XElement.Load(xmlFile); List&lt;PropertyMapping&gt; mappings = new List&lt;PropertyMapping&gt;(); foreach (XElement singleMappingElement in definedMappings.Elements("mapping")) { mappings.Add(new PropertyMapping(singleMappingElement.Element("src").Value, singleMappingElement.Element("dest").Value)); } return mappings; } } </code></pre> <p>My model classes:</p> <pre><code>public class CalendarEvent { public DateTime EventDate { get; set; } public string Title { get; set; } } public class CalendarEventForm { public DateTime EventDate { get; set; } public int EventHour { get; set; } public int EventMinute { get; set; } public string Title { get; set; } public Foo Foo { get; set; } } public class Foo { public Bar Bar { get; set; } } public class Bar { public DateTime InternalDate { get; set; } } </code></pre> <p>Internal mapping representation:</p> <pre><code>public class PropertyMapping { public string FromPropertyName; public string ToPropertyName; public PropertyMapping(string fromPropertyName, string toPropertyName) { this.FromPropertyName = fromPropertyName; this.ToPropertyName = toPropertyName; } } </code></pre> <p>Sample XML configuration:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;ObjectMapping&gt; &lt;mapping&gt; &lt;src&gt;Title&lt;/src&gt; &lt;dest&gt;Title&lt;/dest&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;src&gt;EventDate.Date&lt;/src&gt; &lt;dest&gt;EventDate&lt;/dest&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;src&gt;EventDate.Hour&lt;/src&gt; &lt;dest&gt;EventHour&lt;/dest&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;src&gt;EventDate.Minute&lt;/src&gt; &lt;dest&gt;EventMinute&lt;/dest&gt; &lt;/mapping&gt; &lt;mapping&gt; &lt;src&gt;EventDate&lt;/src&gt; &lt;dest&gt;Foo.Bar.InternalDate&lt;/dest&gt; &lt;/mapping&gt; &lt;/ObjectMapping&gt; </code></pre>
 

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