Skip to content

Commit afafbfe

Browse files
committed
Added new ChangeTypeInvariant methods for convenience and reduced allocation of DeepConvertSettings instances
1 parent 430271e commit afafbfe

File tree

5 files changed

+91
-29
lines changed

5 files changed

+91
-29
lines changed

DeepConvert.Tests/DeepConvert.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.0</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

DeepConvert.Tests/DeepConvertTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public void ChangeType_Numerics()
4141
Assert.AreEqual(100, DeepConvert.ChangeType<double>("100.0", ics));
4242
Assert.AreEqual(100, DeepConvert.ChangeType<decimal>("100"));
4343
Assert.AreEqual(100, DeepConvert.ChangeType<decimal>("100.0", ics));
44+
Assert.AreEqual(100, DeepConvert.ChangeTypeInvariant<decimal>("100.0"));
45+
Assert.AreEqual(100, DeepConvert.ChangeTypeInvariant<decimal>("100.0", decs));
4446
}
4547

4648
[TestMethod]

DeepConvert/DeepConvert.cs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2020, Yves Goergen, https://unclassified.software
1+
// Copyright (c) 2018-2022, Yves Goergen, https://ygoe.de
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
44
// associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -65,6 +65,16 @@ public static partial class DeepConvert
6565
/// </summary>
6666
private static readonly DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
6767

68+
/// <summary>
69+
/// An empty settings instance that can be used to avoid allocation.
70+
/// </summary>
71+
private static readonly DeepConvertSettings emptySettings = new DeepConvertSettings();
72+
73+
/// <summary>
74+
/// A preset settings instance for the invariant culture to avoid allocation.
75+
/// </summary>
76+
private static readonly DeepConvertSettings invariantSettings = new DeepConvertSettings { Provider = CultureInfo.InvariantCulture };
77+
6878
#endregion Private data
6979

7080
#region Main ChangeType methods
@@ -78,7 +88,7 @@ public static partial class DeepConvert
7888
/// <returns>An object whose type is <typeparamref name="T"/> and whose value is equivalent
7989
/// to <paramref name="value"/>.</returns>
8090
public static T ChangeType<T>(object value) =>
81-
(T)ChangeType(value, typeof(T), new DeepConvertSettings());
91+
(T)ChangeType(value, typeof(T), emptySettings);
8292

8393
/// <summary>
8494
/// Returns an object of the specified type whose value is equivalent to the specified
@@ -101,7 +111,7 @@ public static T ChangeType<T>(object value, DeepConvertSettings settings) =>
101111
/// <returns>An object whose type is <paramref name="destType"/> and whose value is
102112
/// equivalent to <paramref name="value"/>.</returns>
103113
public static object ChangeType(object value, Type destType) =>
104-
ChangeType(value, destType, new DeepConvertSettings());
114+
ChangeType(value, destType, emptySettings);
105115

106116
/// <summary>
107117
/// Returns an object of the specified type whose value is equivalent to the specified
@@ -610,6 +620,58 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
610620

611621
#endregion Main ChangeType methods
612622

623+
#region ChangeTypeInvariant methods
624+
625+
/// <summary>
626+
/// Returns an object of the specified type whose value is equivalent to the specified
627+
/// object, using the invariant culture.
628+
/// </summary>
629+
/// <typeparam name="T">The type to convert the data to.</typeparam>
630+
/// <param name="value">The data to convert.</param>
631+
/// <returns>An object whose type is <typeparamref name="T"/> and whose value is equivalent
632+
/// to <paramref name="value"/>.</returns>
633+
public static T ChangeTypeInvariant<T>(object value) =>
634+
(T)ChangeType(value, typeof(T), invariantSettings);
635+
636+
/// <summary>
637+
/// Returns an object of the specified type whose value is equivalent to the specified
638+
/// object, using the invariant culture.
639+
/// </summary>
640+
/// <typeparam name="T">The type to convert the data to.</typeparam>
641+
/// <param name="value">The data to convert.</param>
642+
/// <param name="settings">The conversion settings. The <see cref="DeepConvertSettings.Provider"/>
643+
/// will be overwritten with <see cref="CultureInfo.InvariantCulture"/>.</param>
644+
/// <returns>An object whose type is <typeparamref name="T"/> and whose value is equivalent
645+
/// to <paramref name="value"/>.</returns>
646+
public static T ChangeTypeInvariant<T>(object value, DeepConvertSettings settings) =>
647+
(T)ChangeType(value, typeof(T), new DeepConvertSettings(settings) { Provider = CultureInfo.InvariantCulture });
648+
649+
/// <summary>
650+
/// Returns an object of the specified type whose value is equivalent to the specified
651+
/// object, using the invariant culture.
652+
/// </summary>
653+
/// <param name="value">The data to convert.</param>
654+
/// <param name="destType">The type to convert the data to.</param>
655+
/// <returns>An object whose type is <paramref name="destType"/> and whose value is
656+
/// equivalent to <paramref name="value"/>.</returns>
657+
public static object ChangeTypeInvariant(object value, Type destType) =>
658+
ChangeType(value, destType, invariantSettings);
659+
660+
/// <summary>
661+
/// Returns an object of the specified type whose value is equivalent to the specified
662+
/// object, using the invariant culture.
663+
/// </summary>
664+
/// <param name="value">The data to convert.</param>
665+
/// <param name="destType">The type to convert the data to.</param>
666+
/// <param name="settings">The conversion settings. The <see cref="DeepConvertSettings.Provider"/>
667+
/// will be overwritten with <see cref="CultureInfo.InvariantCulture"/>.</param>
668+
/// <returns>An object whose type is <paramref name="destType"/> and whose value is
669+
/// equivalent to <paramref name="value"/>.</returns>
670+
public static object ChangeTypeInvariant(object value, Type destType, DeepConvertSettings settings) =>
671+
ChangeType(value, destType, new DeepConvertSettings(settings) { Provider = CultureInfo.InvariantCulture });
672+
673+
#endregion ChangeTypeInvariant methods
674+
613675
#region ToDateTime methods
614676

615677
/// <summary>
@@ -618,7 +680,7 @@ public static object ChangeType(object value, Type destType, DeepConvertSettings
618680
/// <param name="value">The data to convert.</param>
619681
/// <returns>A <see cref="DateTime"/> value that is equivalent to <paramref name="value"/>.</returns>
620682
public static DateTime ToDateTime(object value) =>
621-
ToDateTime(value, new DeepConvertSettings());
683+
ToDateTime(value, emptySettings);
622684

623685
/// <summary>
624686
/// Returns a <see cref="DateTime"/> value that is equivalent to the specified object.

DeepConvert/DeepConvert.csproj

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5-
<RootNamespace>Unclassified.Util</RootNamespace>
6-
<AssemblyName>Unclassified.DeepConvert</AssemblyName>
7-
<Version>1.3.0</Version>
8-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
5+
<RootNamespace>Unclassified.Util</RootNamespace>
6+
<AssemblyName>Unclassified.DeepConvert</AssemblyName>
7+
<Version>1.4.0</Version>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99

10-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
11-
<IncludeSymbols>true</IncludeSymbols>
12-
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
13-
<Description>Converts a data type to another data type, including collections and their items as well as object properties (duck typing). With special support for DateTime conversions.</Description>
14-
<Copyright>Yves Goergen</Copyright>
15-
<Authors>Yves Goergen</Authors>
16-
<Company />
17-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
18-
<PackageTags>convert conversion collections data datetime timestamp type cast types duck typing duck-typing</PackageTags>
19-
<PackageProjectUrl>https://github.com/ygoe/DeepConvert</PackageProjectUrl>
20-
<PackageLicenseUrl>https://github.com/ygoe/DeepConvert/blob/master/LICENSE</PackageLicenseUrl>
21-
<RepositoryUrl>https://github.com/ygoe/DeepConvert</RepositoryUrl>
22-
</PropertyGroup>
10+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
11+
<IncludeSymbols>true</IncludeSymbols>
12+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
13+
<Description>Converts a data type to another data type, smarter than the standard Convert class, including collections and their items as well as object properties (duck typing). With special support for DateTime conversions.</Description>
14+
<Copyright>Yves Goergen</Copyright>
15+
<Authors>Yves Goergen</Authors>
16+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17+
<PackageTags>convert conversion collections data datetime timestamp type cast types duck typing duck-typing</PackageTags>
18+
<PackageProjectUrl>https://github.com/ygoe/DeepConvert</PackageProjectUrl>
19+
<RepositoryUrl>https://github.com/ygoe/DeepConvert</RepositoryUrl>
20+
</PropertyGroup>
2321

24-
<ItemGroup>
25-
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
26-
<PackageReference Include="System.Reflection.Emit.ILGeneration" Version="4.3.0" />
27-
</ItemGroup>
22+
<ItemGroup>
23+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
24+
<PackageReference Include="System.Reflection.Emit.ILGeneration" Version="4.3.0" />
25+
</ItemGroup>
2826

2927
</Project>

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2018, Yves Goergen, http://unclassified.software
1+
Copyright (c) 2018-2022, Yves Goergen, https://ygoe.de
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
44
associated documentation files (the "Software"), to deal in the Software without restriction,

0 commit comments

Comments
 (0)