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

Commit 5ae584c

Browse files
committed
Add new CreateCopy method
1 parent 67515ae commit 5ae584c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ public static T ConvertTo<T>(this object from)
4242
return to.PopulateWith(from);
4343
}
4444

45+
public static T CreateCopy<T>(this T from)
46+
{
47+
if (typeof(T).IsValueType())
48+
return (T)ChangeValueType(from, typeof(T));
49+
50+
if (typeof(IEnumerable).IsAssignableFromType(typeof(T)))
51+
{
52+
var listResult = TranslateListWithElements.TryTranslateCollections(
53+
from.GetType(), typeof(T), from);
54+
55+
return (T)listResult;
56+
}
57+
58+
var to = typeof(T).CreateInstance<T>();
59+
return to.PopulateWith(from);
60+
}
61+
4562
public static To ThenDo<To>(this To to, Action<To> fn)
4663
{
4764
fn(to);

0 commit comments

Comments
 (0)