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

Commit b01143e

Browse files
committed
Add ChangeTo API to convert string to target Type
1 parent 9124038 commit b01143e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ private static object ChangeValueType(object from, Type type)
7777
return Convert.ChangeType(from, type, provider: null);
7878
}
7979

80+
public static object ChangeTo(this string strValue, Type type)
81+
{
82+
if (type.IsValueType && !type.IsEnum && type.HasInterface(typeof(IConvertible)))
83+
{
84+
try
85+
{
86+
return Convert.ChangeType(strValue, type);
87+
}
88+
catch (Exception ex)
89+
{
90+
Tracer.Instance.WriteError(ex);
91+
}
92+
}
93+
return TypeSerializer.DeserializeFromString(strValue, type);
94+
}
95+
8096
private static readonly Dictionary<Type, List<string>> TypePropertyNamesMap = new Dictionary<Type, List<string>>();
8197

8298
public static List<string> GetPropertyNames(this Type type)

0 commit comments

Comments
 (0)