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

Commit 000b3c5

Browse files
committed
Add PopulateInstance extension method
1 parent 7ad50a4 commit 000b3c5

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,26 @@ public static object FromObjectDictionary(this IReadOnlyDictionary<string, objec
669669
if (alreadyDict)
670670
return true;
671671

672+
var to = type.CreateInstance();
673+
674+
PopulateInstanceInternal(values, to, type);
675+
676+
return to;
677+
}
678+
679+
public static void PopulateInstance(this IReadOnlyDictionary<string, object> values, object instance)
680+
{
681+
if (values == null || instance == null)
682+
return;
683+
684+
PopulateInstanceInternal(values, instance, instance.GetType());
685+
}
686+
687+
private static void PopulateInstanceInternal(IReadOnlyDictionary<string, object> values, object to, Type type)
688+
{
672689
if (!toObjectMapCache.TryGetValue(type, out var def))
673690
toObjectMapCache[type] = def = CreateObjectDictionaryDefinition(type);
674691

675-
var to = type.CreateInstance();
676692
foreach (var entry in values)
677693
{
678694
if (!def.FieldsMap.TryGetValue(entry.Key, out var fieldDef) &&
@@ -682,7 +698,6 @@ public static object FromObjectDictionary(this IReadOnlyDictionary<string, objec
682698

683699
fieldDef.SetValue(to, entry.Value);
684700
}
685-
return to;
686701
}
687702

688703
public static T FromObjectDictionary<T>(this IReadOnlyDictionary<string, object> values)

0 commit comments

Comments
 (0)