最后,我自己实现了最初的需求,尽管我并不需要它(需求改变了)。我将在此处提供代码以防有人需要它(或多或少作为概念证明,因为仍然可以进行很多改进)或者对 XML 容易出错感兴趣,请记住,这种方法中的关键组件,属性名称和类型必须完全匹配,否则它将无法工作,但是使用一点 GUI 来编辑文件应该是可以实现的(我的意思是不手动编辑文件)。
我使用了here和here的代码,并添加了PropertyMapping类来存储从XML读取的映射,还添加了Foo和Bar类来创建嵌套数据结构,以便复制到。
无论如何这里是代码,也许它可以帮助某人一段时间:
主要的:
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) &&
calendarEvent.EventDate.Hour.Equals(form.EventHour) &&
calendarEvent.EventDate.Minute.Equals(form.EventMinute) &&
calendarEvent.Title.Equals(form.Title);
}
}
映射器实现:
public class MyObjectMapper
{
private List<PropertyMapping> myMappings = new List<PropertyMapping>();
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 < 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<PropertyMapping> GenerateMappingObjectsFromXml(string xmlFile)
{
XElement definedMappings = XElement.Load(xmlFile);
List<PropertyMapping> mappings = new List<PropertyMapping>();
foreach (XElement singleMappingElement in definedMappings.Elements("mapping"))
{
mappings.Add(new PropertyMapping(singleMappingElement.Element("src").Value, singleMappingElement.Element("dest").Value));
}
return mappings;
}
}
我的模型类:
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; }
}
内部映射表示:
public class PropertyMapping
{
public string FromPropertyName;
public string ToPropertyName;
public PropertyMapping(string fromPropertyName, string toPropertyName)
{
this.FromPropertyName = fromPropertyName;
this.ToPropertyName = toPropertyName;
}
}
示例 XML 配置:
<?xml version="1.0" encoding="utf-8" ?>
<ObjectMapping>
<mapping>
<src>Title</src>
<dest>Title</dest>
</mapping>
<mapping>
<src>EventDate.Date</src>
<dest>EventDate</dest>
</mapping>
<mapping>
<src>EventDate.Hour</src>
<dest>EventHour</dest>
</mapping>
<mapping>
<src>EventDate.Minute</src>
<dest>EventMinute</dest>
</mapping>
<mapping>
<src>EventDate</src>
<dest>Foo.Bar.InternalDate</dest>
</mapping>
</ObjectMapping>