Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c40ecde

Browse files
committed
Add PopulateInstance
1 parent c8923be commit c40ecde

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -527,19 +527,6 @@ public static To PopulateWith<To, From>(this To to, From from)
527527
return to;
528528
}
529529

530-
[Obsolete("Use PopulateWithNonDefaultValues")]
531-
public static object PopulateInstance(this object to, object from)
532-
{
533-
if (to == null || from == null)
534-
return null;
535-
536-
var assignmentDefinition = GetAssignmentDefinition(to.GetType(), from.GetType());
537-
538-
assignmentDefinition.PopulateWithNonDefaultValues(to, from);
539-
540-
return to;
541-
}
542-
543530
public static To PopulateWithNonDefaultValues<To, From>(this To to, From from)
544531
{
545532
if (Equals(to, default(To)) || Equals(from, default(From))) return default(To);

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,30 @@ private static void PopulateInstanceInternal(IEnumerable<KeyValuePair<string, ob
973973
}
974974
}
975975

976+
public static void PopulateInstance(this IEnumerable<KeyValuePair<string, string>> values, object instance)
977+
{
978+
if (values == null || instance == null)
979+
return;
980+
981+
PopulateInstanceInternal(values, instance, instance.GetType());
982+
}
983+
984+
private static void PopulateInstanceInternal(IEnumerable<KeyValuePair<string, string>> values, object to, Type type)
985+
{
986+
if (!toObjectMapCache.TryGetValue(type, out var def))
987+
toObjectMapCache[type] = def = CreateObjectDictionaryDefinition(type);
988+
989+
foreach (var entry in values)
990+
{
991+
if (!def.FieldsMap.TryGetValue(entry.Key, out var fieldDef) &&
992+
!def.FieldsMap.TryGetValue(entry.Key.ToPascalCase(), out fieldDef)
993+
|| entry.Value == null)
994+
continue;
995+
996+
fieldDef.SetValue(to, entry.Value);
997+
}
998+
}
999+
9761000
public static T FromObjectDictionary<T>(this IEnumerable<KeyValuePair<string, object>> values)
9771001
{
9781002
return (T)values.FromObjectDictionary(typeof(T));

0 commit comments

Comments
 (0)