From 086f278e8444634b0893032b78fa3dedcee30432 Mon Sep 17 00:00:00 2001 From: "grzegorz.russek" Date: Tue, 13 Sep 2022 08:44:52 +0000 Subject: [PATCH] --- AmalgamationTool/DynamORM.Amalgamation.cs | 17 +++++++++++------ DynamORM/Mapper/DynamicPropertyInvoker.cs | 15 ++++++++++----- DynamORM/Mapper/DynamicTypeMap.cs | 2 +- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/AmalgamationTool/DynamORM.Amalgamation.cs b/AmalgamationTool/DynamORM.Amalgamation.cs index 0a8c80b..3915a0c 100644 --- a/AmalgamationTool/DynamORM.Amalgamation.cs +++ b/AmalgamationTool/DynamORM.Amalgamation.cs @@ -13550,7 +13550,7 @@ namespace DynamORM /// Sets the specified value to destination object. /// The destination object. /// The value. - public void Set(object dest, object val) + public void Set(object dest, object val, bool byProperty = false) { object value = null; @@ -13564,7 +13564,7 @@ namespace DynamORM { if (val is IEnumerable) { - var lst = (val as IEnumerable).Select(x => GetElementVal(ArrayType, x)).ToList(); + var lst = (val as IEnumerable).Select(x => GetElementVal(ArrayType, x, byProperty)).ToList(); value = Array.CreateInstance(ArrayType, lst.Count); @@ -13575,14 +13575,14 @@ namespace DynamORM else { value = Array.CreateInstance(ArrayType, 1); - ((Array)value).SetValue(GetElementVal(ArrayType, val), 0); + ((Array)value).SetValue(GetElementVal(ArrayType, val, byProperty), 0); } } else value = Array.CreateInstance(ArrayType, 0); } else - value = GetElementVal(Type, val); + value = GetElementVal(Type, val, byProperty); } else value = val; @@ -13598,7 +13598,7 @@ namespace DynamORM } } - private object GetElementVal(System.Type etype, object val) + private object GetElementVal(System.Type etype, object val, bool byProperty) { bool nullable = etype.IsGenericType && etype.GetGenericTypeDefinition() == typeof(Nullable<>); Type type = Nullable.GetUnderlyingType(etype) ?? etype; @@ -13640,7 +13640,12 @@ namespace DynamORM else return (nullable) ? null : (object)Guid.Empty; } else if (!typeof(IConvertible).IsAssignableFrom(type) && (IsDataContract || (!type.IsValueType && val is IDictionary))) + { + if (byProperty) + return val.MapByProperty(type); + return val.Map(type); + } else try { @@ -13800,7 +13805,7 @@ namespace DynamORM if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null) if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null) if (dpi.Setter != null) - dpi.Set(destination, item.Value); + dpi.Set(destination, item.Value, true); } return destination; diff --git a/DynamORM/Mapper/DynamicPropertyInvoker.cs b/DynamORM/Mapper/DynamicPropertyInvoker.cs index cb0abb1..31a4c26 100644 --- a/DynamORM/Mapper/DynamicPropertyInvoker.cs +++ b/DynamORM/Mapper/DynamicPropertyInvoker.cs @@ -156,7 +156,7 @@ namespace DynamORM.Mapper /// Sets the specified value to destination object. /// The destination object. /// The value. - public void Set(object dest, object val) + public void Set(object dest, object val, bool byProperty = false) { object value = null; @@ -170,7 +170,7 @@ namespace DynamORM.Mapper { if (val is IEnumerable) { - var lst = (val as IEnumerable).Select(x => GetElementVal(ArrayType, x)).ToList(); + var lst = (val as IEnumerable).Select(x => GetElementVal(ArrayType, x, byProperty)).ToList(); value = Array.CreateInstance(ArrayType, lst.Count); @@ -181,14 +181,14 @@ namespace DynamORM.Mapper else { value = Array.CreateInstance(ArrayType, 1); - ((Array)value).SetValue(GetElementVal(ArrayType, val), 0); + ((Array)value).SetValue(GetElementVal(ArrayType, val, byProperty), 0); } } else value = Array.CreateInstance(ArrayType, 0); } else - value = GetElementVal(Type, val); + value = GetElementVal(Type, val, byProperty); } else value = val; @@ -204,7 +204,7 @@ namespace DynamORM.Mapper } } - private object GetElementVal(System.Type etype, object val) + private object GetElementVal(System.Type etype, object val, bool byProperty) { bool nullable = etype.IsGenericType && etype.GetGenericTypeDefinition() == typeof(Nullable<>); Type type = Nullable.GetUnderlyingType(etype) ?? etype; @@ -246,7 +246,12 @@ namespace DynamORM.Mapper else return (nullable) ? null : (object)Guid.Empty; } else if (!typeof(IConvertible).IsAssignableFrom(type) && (IsDataContract || (!type.IsValueType && val is IDictionary))) + { + if (byProperty) + return val.MapByProperty(type); + return val.Map(type); + } else try { diff --git a/DynamORM/Mapper/DynamicTypeMap.cs b/DynamORM/Mapper/DynamicTypeMap.cs index ddd1a55..be2604c 100644 --- a/DynamORM/Mapper/DynamicTypeMap.cs +++ b/DynamORM/Mapper/DynamicTypeMap.cs @@ -171,7 +171,7 @@ namespace DynamORM.Mapper if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null) if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null) if (dpi.Setter != null) - dpi.Set(destination, item.Value); + dpi.Set(destination, item.Value, true); } return destination;