1: public class Mapping<Source, Destination>:IMapping
2: {
3: public Mapping()
4: {
5: Setup();
6: }
7:
8: public void Sync(object SourceObject, object DestinationObject)
9: {
10: foreach(string Key in Mappings.Keys)
11: {
12: string[]Splitter={"."};
13: string[]SourceProperties=Key.Split(Splitter,StringSplitOptions.None);
14: object TempSourceProperty = SourceObject;
15: Type PropertyType=SourceType;
16: for (int x = 0; x < SourceProperties.Length; ++x)
17: {
18: TempSourceProperty = PropertyType.GetProperty(SourceProperties[x]).GetValue(TempSourceProperty, null);
19: PropertyType = TempSourceProperty.GetType();
20: }
21: string[] DestinationProperties = Mappings[Key].Split(Splitter, StringSplitOptions.None);
22: object TempDestinationProperty = DestinationObject;
23: Type DestinationPropertyType = DestinationType;
24: for (int x = 0; x < DestinationProperties.Length-1; ++x)
25: {
26: TempDestinationProperty = DestinationPropertyType.GetProperty(DestinationProperties[x]).GetValue(TempDestinationProperty, null);
27: PropertyType = TempSourceProperty.GetType();
28: }
29: DestinationPropertyType.GetProperty(DestinationProperties[DestinationProperties.Length-1]).SetValue(TempDestinationProperty,TempSourceProperty,null);
30: }
31: }
32:
33: protected void Map(Expression<Func<Source, object>> SourceExpression,
34: Expression<Func<Destination, object>> DestinationExpression)
35: {
36: Setup();
37: string SourceName = GetName<Source>(SourceExpression);
38: string DestinationName = GetName<Destination>(DestinationExpression);
39: if (Mappings.ContainsValue(DestinationName))
40: {
41: foreach (string Key in Mappings.Keys)
42: {
43: if (Mappings[Key] == DestinationName)
44: {
45: Mappings.Remove(Key);
46: break;
47: }
48: }
49: }
50: if (Mappings.ContainsKey(SourceName))
51: {
52: Mappings[SourceName] = DestinationName;
53: }
54: else
55: {
56: Mappings.Add(SourceName, DestinationName);
57: }
58: }
59:
60: protected void Ignore(Expression<Func<Source, object>> SourceExpression)
61: {
62: Setup();
63: string SourceName = GetName<Source>(SourceExpression);
64: if (Mappings.ContainsKey(SourceName))
65: {
66: Mappings.Remove(SourceName);
67: }
68: }
69:
70: private void Setup()
71: {
72: if (Mappings == null)
73: {
74: Mappings = new Dictionary<string, string>();
75: PropertyInfo[] Properties = typeof(Source).GetProperties();
76: Type DestinationType = typeof(Destination);
77: for (int x = 0; x < Properties.Length; ++x)
78: {
79: if (DestinationType.GetProperty(Properties[x].Name) != null)
80: {
81: Mappings.Add(Properties[x].Name, Properties[x].Name);
82: }
83: }
84: }
85: }
86:
87: private string GetName<T>(Expression<Func<T, object>> expression)
88: {
89: string Name = "";
90: if (expression.Body.NodeType == ExpressionType.Convert)
91: {
92: Name = expression.Body.ToString().Replace("Convert(", "").Replace(")", "");
93: Name = Name.Remove(0, Name.IndexOf(".") + 1);
94: }
95: else
96: {
97: Name = expression.Body.ToString();
98: Name = Name.Remove(0, Name.IndexOf(".") + 1);
99: }
100: return Name;
101: }
102:
103: public Type SourceType { get { return typeof(Source); } }
104: public Type DestinationType { get { return typeof(Destination); } }
105:
106: protected Dictionary<string, string> Mappings { get; set; }
107: }