diff --git a/lab/Z.ExtensionMethods.Lab/Form1.cs b/lab/Z.ExtensionMethods.Lab/Form1.cs index cd832912..34419fd1 100644 --- a/lab/Z.ExtensionMethods.Lab/Form1.cs +++ b/lab/Z.ExtensionMethods.Lab/Form1.cs @@ -157,25 +157,18 @@ public partial class Form1 : Form public Form1() { InitializeComponent(); + object x1 = null; - var aaaa = (Attribute)null; - - StringBuilder sb = new StringBuilder("0123456789"); - var b4 = "abc".Substring(1, 4); - char ch = '\''; - char ch2 = '"'; - string tag = "acd"; - string tag2 = tag.StripHtml(); - var a = new[] {"a", "b", "c"}; - var b = new[] {"c", "d"}; - - using (var command = new SqlCommand()) - { - // command.ExecuteDataSet(); - } + var r1 = x1.ToInt32OrDefault(4); + var r2 = x1.ToInt32OrDefault(4, true); + + //using (var command = new SqlCommand()) + //{ + // // command.ExecuteDataSet(); + //} - var c = new List {a, b}; - IEnumerable d = c.MergeInnerEnumerable(); + //var c = new List {a, b}; + //IEnumerable d = c.MergeInnerEnumerable(); } public void Misc() diff --git a/lab/Z.ExtensionMethods.Lab/Z.ExtensionMethods.Lab.csproj b/lab/Z.ExtensionMethods.Lab/Z.ExtensionMethods.Lab.csproj index 14db5750..f1b7a742 100644 --- a/lab/Z.ExtensionMethods.Lab/Z.ExtensionMethods.Lab.csproj +++ b/lab/Z.ExtensionMethods.Lab/Z.ExtensionMethods.Lab.csproj @@ -50,9 +50,6 @@ - - ..\..\..\..\..\..\OneDrive\Documents\Release\Z.ExtensionMethods\Z.ExtensionMethods.WithNamespace\NET40\Z.ExtensionMethods.WithNamespace.dll - @@ -86,6 +83,10 @@ + + {02f009f8-720b-4cbd-98ff-f2c33f4441d5} + Z.Core + {40cec7db-c8e6-4a24-8de7-9e0edf8708c6} Z.Data.SQLite diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToBooleanOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToBooleanOrDefault.cs index 211d1a06..fb1bb9cc 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToBooleanOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToBooleanOrDefault.cs @@ -42,6 +42,29 @@ public static bool ToBooleanOrDefault(this object @this, bool defaultValue) return defaultValue; } } + /// + /// An object extension method that converts this object to a boolean or default. + /// + /// The @this to act on. + /// true to default value. + /// true to use default if null. + /// The given data converted to a bool. + public static bool ToBooleanOrDefault(this object @this, bool defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToBoolean(@this); + } + catch (Exception) + { + return defaultValue; + } + } /// /// An object extension method that converts this object to a boolean or default. @@ -60,4 +83,28 @@ public static bool ToBooleanOrDefault(this object @this, Func defaultValue return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a boolean or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a bool. + public static bool ToBooleanOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToBoolean(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToByteOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToByteOrDefault.cs index 9dc2e2c3..aae795d2 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToByteOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToByteOrDefault.cs @@ -43,6 +43,28 @@ public static byte ToByteOrDefault(this object @this, byte defaultValue) } } + /// An object extension method that converts this object to a byte or default. + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a byte. + public static byte ToByteOrDefault(this object @this, byte defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToByte(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a byte or default. /// @@ -60,4 +82,26 @@ public static byte ToByteOrDefault(this object @this, Func defaultValueFac return defaultValueFactory(); } } + + /// An object extension method that converts this object to a byte or default. + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a byte. + public static byte ToByteOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToByte(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToCharOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToCharOrDefault.cs index cfbfdf19..9f5bf259 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToCharOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToCharOrDefault.cs @@ -43,6 +43,30 @@ public static char ToCharOrDefault(this object @this, char defaultValue) } } + /// + /// An object extension method that converts this object to a character or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a char. + public static char ToCharOrDefault(this object @this, char defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToChar(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a character or default. /// @@ -60,4 +84,28 @@ public static char ToCharOrDefault(this object @this, Func defaultValueFac return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a character or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a char. + public static char ToCharOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToChar(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOffSetOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOffSetOrDefault.cs index c0dfd821..bd726f22 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOffSetOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOffSetOrDefault.cs @@ -43,6 +43,30 @@ public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, DateTi } } + /// + /// An object extension method that converts this object to a date time off set or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a DateTimeOffset. + public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, DateTimeOffset defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return new DateTimeOffset(Convert.ToDateTime(@this), TimeSpan.Zero); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a date time off set or default. /// @@ -60,4 +84,28 @@ public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, Func + /// An object extension method that converts this object to a date time off set or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a DateTimeOffset. + public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return new DateTimeOffset(Convert.ToDateTime(@this), TimeSpan.Zero); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOrDefault.cs index a02c1acf..8714d3cd 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOrDefault.cs @@ -43,6 +43,30 @@ public static DateTime ToDateTimeOrDefault(this object @this, DateTime defaultVa } } + /// + /// An object extension method that converts this object to a date time or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a DateTime. + public static DateTime ToDateTimeOrDefault(this object @this, DateTime defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToDateTime(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a date time or default. /// @@ -60,4 +84,28 @@ public static DateTime ToDateTimeOrDefault(this object @this, Func def return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a date time or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a DateTime. + public static DateTime ToDateTimeOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToDateTime(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDecimalOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDecimalOrDefault.cs index c5e7e014..f73b0742 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDecimalOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDecimalOrDefault.cs @@ -43,6 +43,30 @@ public static decimal ToDecimalOrDefault(this object @this, decimal defaultValue } } + /// + /// An object extension method that converts this object to a decimal or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a decimal. + public static decimal ToDecimalOrDefault(this object @this, decimal defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToDecimal(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a decimal or default. /// @@ -60,4 +84,28 @@ public static decimal ToDecimalOrDefault(this object @this, Func defaul return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a decimal or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a decimal. + public static decimal ToDecimalOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToDecimal(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDoubleOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDoubleOrDefault.cs index 543eb83c..ad2ca813 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDoubleOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToDoubleOrDefault.cs @@ -43,6 +43,30 @@ public static double ToDoubleOrDefault(this object @this, double defaultValue) } } + /// + /// An object extension method that converts this object to a double or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a double. + public static double ToDoubleOrDefault(this object @this, double defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToDouble(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a double or default. /// @@ -60,4 +84,28 @@ public static double ToDoubleOrDefault(this object @this, Func defaultVa return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a double or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a double. + public static double ToDoubleOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToDouble(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToFloatOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToFloatOrDefault.cs index 2a0c7656..c6160bd4 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToFloatOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToFloatOrDefault.cs @@ -43,6 +43,30 @@ public static float ToFloatOrDefault(this object @this, float defaultValue) } } + /// + /// An object extension method that converts this object to a float or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a float. + public static float ToFloatOrDefault(this object @this, float defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToSingle(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a float or default. /// @@ -60,4 +84,28 @@ public static float ToFloatOrDefault(this object @this, Func defaultValue return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a float or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a float. + public static float ToFloatOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToSingle(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToGuidOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToGuidOrDefault.cs index 763ac9b0..69321da9 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToGuidOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToGuidOrDefault.cs @@ -43,6 +43,30 @@ public static Guid ToGuidOrDefault(this object @this, Guid defaultValue) } } + /// + /// An object extension method that converts this object to a unique identifier or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a GUID. + public static Guid ToGuidOrDefault(this object @this, Guid defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return new Guid(@this.ToString()); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a unique identifier or default. /// @@ -60,4 +84,28 @@ public static Guid ToGuidOrDefault(this object @this, Func defaultValueFac return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a unique identifier or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a GUID. + public static Guid ToGuidOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return new Guid(@this.ToString()); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt16OrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt16OrDefault.cs index 10d3ca44..3fd486eb 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt16OrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt16OrDefault.cs @@ -43,6 +43,30 @@ public static short ToInt16OrDefault(this object @this, short defaultValue) } } + /// + /// An object extension method that converts this object to an int 16 or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a short. + public static short ToInt16OrDefault(this object @this, short defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToInt16(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an int 16 or default. /// @@ -60,4 +84,28 @@ public static short ToInt16OrDefault(this object @this, Func defaultValue return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an int 16 or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a short. + public static short ToInt16OrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToInt16(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt32OrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt32OrDefault.cs index 62519158..2e4ed46a 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt32OrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt32OrDefault.cs @@ -43,6 +43,30 @@ public static int ToInt32OrDefault(this object @this, int defaultValue) } } + /// + /// An object extension method that converts this object to an int 32 or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to an int. + public static int ToInt32OrDefault(this object @this, int defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToInt32(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an int 32 or default. /// @@ -60,4 +84,28 @@ public static int ToInt32OrDefault(this object @this, Func defaultValueFact return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an int 32 or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to an int. + public static int ToInt32OrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToInt32(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt64OrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt64OrDefault.cs index 0b4b520d..4fc74e2b 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt64OrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToInt64OrDefault.cs @@ -43,6 +43,30 @@ public static long ToInt64OrDefault(this object @this, long defaultValue) } } + /// + /// An object extension method that converts this object to an int 64 or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a long. + public static long ToInt64OrDefault(this object @this, long defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToInt64(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an int 64 or default. /// @@ -60,4 +84,28 @@ public static long ToInt64OrDefault(this object @this, Func defaultValueFa return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an int 64 or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a long. + public static long ToInt64OrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToInt64(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToLongOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToLongOrDefault.cs index 7e3f41cc..e9f4b51e 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToLongOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToLongOrDefault.cs @@ -43,6 +43,28 @@ public static long ToLongOrDefault(this object @this, long defaultValue) } } + /// An object extension method that converts this object to a long or default. + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a long. + public static long ToLongOrDefault(this object @this, long defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToInt64(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a long or default. /// @@ -60,4 +82,26 @@ public static long ToLongOrDefault(this object @this, Func defaultValueFac return defaultValueFactory(); } } + + /// An object extension method that converts this object to a long or default. + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a long. + public static long ToLongOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToInt64(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSByteOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSByteOrDefault.cs index e696e323..8e2826ca 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSByteOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSByteOrDefault.cs @@ -43,6 +43,30 @@ public static sbyte ToSByteOrDefault(this object @this, sbyte defaultValue) } } + /// + /// An object extension method that converts this object to the s byte or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a sbyte. + public static sbyte ToSByteOrDefault(this object @this, sbyte defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToSByte(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to the s byte or default. /// @@ -60,4 +84,28 @@ public static sbyte ToSByteOrDefault(this object @this, Func defaultValue return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to the s byte or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a sbyte. + public static sbyte ToSByteOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToSByte(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToShortOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToShortOrDefault.cs index c3751f30..39ba123d 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToShortOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToShortOrDefault.cs @@ -43,6 +43,30 @@ public static short ToShortOrDefault(this object @this, short defaultValue) } } + /// + /// An object extension method that converts this object to a short or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a short. + public static short ToShortOrDefault(this object @this, short defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToInt16(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a short or default. /// @@ -60,4 +84,28 @@ public static short ToShortOrDefault(this object @this, Func defaultValue return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a short or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a short. + public static short ToShortOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToInt16(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSingleOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSingleOrDefault.cs index f6ae7312..767772e8 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSingleOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToSingleOrDefault.cs @@ -43,6 +43,30 @@ public static float ToSingleOrDefault(this object @this, float defaultValue) } } + /// + /// An object extension method that converts this object to a single or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a float. + public static float ToSingleOrDefault(this object @this, float defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToSingle(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a single or default. /// @@ -60,4 +84,28 @@ public static float ToSingleOrDefault(this object @this, Func defaultValu return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a single or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a float. + public static float ToSingleOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToSingle(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToStringOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToStringOrDefault.cs index ec1df75a..ced4c6d4 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToStringOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToStringOrDefault.cs @@ -43,6 +43,30 @@ public static string ToStringOrDefault(this object @this, string defaultValue) } } + /// + /// An object extension method that converts this object to a string or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to a string. + public static string ToStringOrDefault(this object @this, string defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToString(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to a string or default. /// @@ -60,4 +84,28 @@ public static string ToStringOrDefault(this object @this, Func defaultVa return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to a string or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to a string. + public static string ToStringOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToString(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt16OrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt16OrDefault.cs index ddbf6c36..1e0e5317 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt16OrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt16OrDefault.cs @@ -43,6 +43,30 @@ public static ushort ToUInt16OrDefault(this object @this, ushort defaultValue) } } + /// + /// An object extension method that converts this object to an u int 16 or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to an ushort. + public static ushort ToUInt16OrDefault(this object @this, ushort defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToUInt16(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an u int 16 or default. /// @@ -60,4 +84,28 @@ public static ushort ToUInt16OrDefault(this object @this, Func defaultVa return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an u int 16 or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to an ushort. + public static ushort ToUInt16OrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToUInt16(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt32OrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt32OrDefault.cs index e640c2a0..38a2128b 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt32OrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt32OrDefault.cs @@ -43,6 +43,30 @@ public static uint ToUInt32OrDefault(this object @this, uint defaultValue) } } + /// + /// An object extension method that converts this object to an u int 32 or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to an uint. + public static uint ToUInt32OrDefault(this object @this, uint defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToUInt32(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an u int 32 or default. /// @@ -60,4 +84,28 @@ public static uint ToUInt32OrDefault(this object @this, Func defaultValueF return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an u int 32 or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to an uint. + public static uint ToUInt32OrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToUInt32(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt64OrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt64OrDefault.cs index 8ff5b0c7..bdca8ae9 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt64OrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUInt64OrDefault.cs @@ -43,6 +43,30 @@ public static ulong ToUInt64OrDefault(this object @this, ulong defaultValue) } } + /// + /// An object extension method that converts this object to an u int 64 or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to an ulong. + public static ulong ToUInt64OrDefault(this object @this, ulong defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToUInt64(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an u int 64 or default. /// @@ -60,4 +84,28 @@ public static ulong ToUInt64OrDefault(this object @this, Func defaultValu return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an u int 64 or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to an ulong. + public static ulong ToUInt64OrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToUInt64(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToULongOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToULongOrDefault.cs index 5b6c55a0..dcd4e376 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToULongOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToULongOrDefault.cs @@ -43,6 +43,30 @@ public static ulong ToULongOrDefault(this object @this, ulong defaultValue) } } + /// + /// An object extension method that converts this object to an u long or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to an ulong. + public static ulong ToULongOrDefault(this object @this, ulong defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToUInt64(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an u long or default. /// @@ -60,4 +84,28 @@ public static ulong ToULongOrDefault(this object @this, Func defaultValue return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an u long or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to an ulong. + public static ulong ToULongOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToUInt64(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file diff --git a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUShortOrDefault.cs b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUShortOrDefault.cs index b17bcab6..0efb961b 100644 --- a/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUShortOrDefault.cs +++ b/src/Z.Core/System.Object/Convert/ToValueType/Object.ToUShortOrDefault.cs @@ -43,6 +43,30 @@ public static ushort ToUShortOrDefault(this object @this, ushort defaultValue) } } + /// + /// An object extension method that converts this object to an u short or default. + /// + /// The @this to act on. + /// The default value. + /// true to use default if null. + /// The given data converted to an ushort. + public static ushort ToUShortOrDefault(this object @this, ushort defaultValue, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValue; + } + + try + { + return Convert.ToUInt16(@this); + } + catch (Exception) + { + return defaultValue; + } + } + /// /// An object extension method that converts this object to an u short or default. /// @@ -60,4 +84,28 @@ public static ushort ToUShortOrDefault(this object @this, Func defaultVa return defaultValueFactory(); } } + + /// + /// An object extension method that converts this object to an u short or default. + /// + /// The @this to act on. + /// The default value factory. + /// true to use default if null. + /// The given data converted to an ushort. + public static ushort ToUShortOrDefault(this object @this, Func defaultValueFactory, bool useDefaultIfNull) + { + if (useDefaultIfNull && @this == null) + { + return defaultValueFactory(); + } + + try + { + return Convert.ToUInt16(@this); + } + catch (Exception) + { + return defaultValueFactory(); + } + } } \ No newline at end of file