This commit is contained in:
grzegorz.russek
2022-09-13 08:44:52 +00:00
parent e8c0224a03
commit 086f278e84
3 changed files with 22 additions and 12 deletions

View File

@@ -13550,7 +13550,7 @@ namespace DynamORM
/// <summary>Sets the specified value to destination object.</summary> /// <summary>Sets the specified value to destination object.</summary>
/// <param name="dest">The destination object.</param> /// <param name="dest">The destination object.</param>
/// <param name="val">The value.</param> /// <param name="val">The value.</param>
public void Set(object dest, object val) public void Set(object dest, object val, bool byProperty = false)
{ {
object value = null; object value = null;
@@ -13564,7 +13564,7 @@ namespace DynamORM
{ {
if (val is IEnumerable<object>) if (val is IEnumerable<object>)
{ {
var lst = (val as IEnumerable<object>).Select(x => GetElementVal(ArrayType, x)).ToList(); var lst = (val as IEnumerable<object>).Select(x => GetElementVal(ArrayType, x, byProperty)).ToList();
value = Array.CreateInstance(ArrayType, lst.Count); value = Array.CreateInstance(ArrayType, lst.Count);
@@ -13575,14 +13575,14 @@ namespace DynamORM
else else
{ {
value = Array.CreateInstance(ArrayType, 1); value = Array.CreateInstance(ArrayType, 1);
((Array)value).SetValue(GetElementVal(ArrayType, val), 0); ((Array)value).SetValue(GetElementVal(ArrayType, val, byProperty), 0);
} }
} }
else else
value = Array.CreateInstance(ArrayType, 0); value = Array.CreateInstance(ArrayType, 0);
} }
else else
value = GetElementVal(Type, val); value = GetElementVal(Type, val, byProperty);
} }
else else
value = val; 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<>); bool nullable = etype.IsGenericType && etype.GetGenericTypeDefinition() == typeof(Nullable<>);
Type type = Nullable.GetUnderlyingType(etype) ?? etype; Type type = Nullable.GetUnderlyingType(etype) ?? etype;
@@ -13640,7 +13640,12 @@ namespace DynamORM
else return (nullable) ? null : (object)Guid.Empty; else return (nullable) ? null : (object)Guid.Empty;
} }
else if (!typeof(IConvertible).IsAssignableFrom(type) && (IsDataContract || (!type.IsValueType && val is IDictionary<string, object>))) else if (!typeof(IConvertible).IsAssignableFrom(type) && (IsDataContract || (!type.IsValueType && val is IDictionary<string, object>)))
{
if (byProperty)
return val.MapByProperty(type);
return val.Map(type); return val.Map(type);
}
else else
try try
{ {
@@ -13800,7 +13805,7 @@ namespace DynamORM
if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null) if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null)
if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null) if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null)
if (dpi.Setter != null) if (dpi.Setter != null)
dpi.Set(destination, item.Value); dpi.Set(destination, item.Value, true);
} }
return destination; return destination;

View File

@@ -156,7 +156,7 @@ namespace DynamORM.Mapper
/// <summary>Sets the specified value to destination object.</summary> /// <summary>Sets the specified value to destination object.</summary>
/// <param name="dest">The destination object.</param> /// <param name="dest">The destination object.</param>
/// <param name="val">The value.</param> /// <param name="val">The value.</param>
public void Set(object dest, object val) public void Set(object dest, object val, bool byProperty = false)
{ {
object value = null; object value = null;
@@ -170,7 +170,7 @@ namespace DynamORM.Mapper
{ {
if (val is IEnumerable<object>) if (val is IEnumerable<object>)
{ {
var lst = (val as IEnumerable<object>).Select(x => GetElementVal(ArrayType, x)).ToList(); var lst = (val as IEnumerable<object>).Select(x => GetElementVal(ArrayType, x, byProperty)).ToList();
value = Array.CreateInstance(ArrayType, lst.Count); value = Array.CreateInstance(ArrayType, lst.Count);
@@ -181,14 +181,14 @@ namespace DynamORM.Mapper
else else
{ {
value = Array.CreateInstance(ArrayType, 1); value = Array.CreateInstance(ArrayType, 1);
((Array)value).SetValue(GetElementVal(ArrayType, val), 0); ((Array)value).SetValue(GetElementVal(ArrayType, val, byProperty), 0);
} }
} }
else else
value = Array.CreateInstance(ArrayType, 0); value = Array.CreateInstance(ArrayType, 0);
} }
else else
value = GetElementVal(Type, val); value = GetElementVal(Type, val, byProperty);
} }
else else
value = val; 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<>); bool nullable = etype.IsGenericType && etype.GetGenericTypeDefinition() == typeof(Nullable<>);
Type type = Nullable.GetUnderlyingType(etype) ?? etype; Type type = Nullable.GetUnderlyingType(etype) ?? etype;
@@ -246,7 +246,12 @@ namespace DynamORM.Mapper
else return (nullable) ? null : (object)Guid.Empty; else return (nullable) ? null : (object)Guid.Empty;
} }
else if (!typeof(IConvertible).IsAssignableFrom(type) && (IsDataContract || (!type.IsValueType && val is IDictionary<string, object>))) else if (!typeof(IConvertible).IsAssignableFrom(type) && (IsDataContract || (!type.IsValueType && val is IDictionary<string, object>)))
{
if (byProperty)
return val.MapByProperty(type);
return val.Map(type); return val.Map(type);
}
else else
try try
{ {

View File

@@ -171,7 +171,7 @@ namespace DynamORM.Mapper
if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null) if (PropertyMap.TryGetValue(item.Key, out cn) && item.Value != null)
if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null) if (ColumnsMap.TryGetValue(cn.ToLower(), out dpi) && item.Value != null)
if (dpi.Setter != null) if (dpi.Setter != null)
dpi.Set(destination, item.Value); dpi.Set(destination, item.Value, true);
} }
return destination; return destination;