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.
0 commit comments