Skip to content

Commit f6fd4ed

Browse files
author
zzzprojects
committed
Add overload useDefaultIfNull
Add overload useDefaultIfNull
1 parent 2411aa4 commit f6fd4ed

24 files changed

+1061
-20
lines changed

lab/Z.ExtensionMethods.Lab/Form1.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,18 @@ public partial class Form1 : Form
157157
public Form1()
158158
{
159159
InitializeComponent();
160+
object x1 = null;
160161

161-
var aaaa = (Attribute)null;
162-
163-
StringBuilder sb = new StringBuilder("0123456789");
164-
var b4 = "abc".Substring(1, 4);
165-
char ch = '\'';
166-
char ch2 = '"';
167-
string tag = "a<b class='to>to'>c</b>d";
168-
string tag2 = tag.StripHtml();
169-
var a = new[] {"a", "b", "c"};
170-
var b = new[] {"c", "d"};
171-
172-
using (var command = new SqlCommand())
173-
{
174-
// command.ExecuteDataSet();
175-
}
162+
var r1 = x1.ToInt32OrDefault(4);
163+
var r2 = x1.ToInt32OrDefault(4, true);
164+
165+
//using (var command = new SqlCommand())
166+
//{
167+
// // command.ExecuteDataSet();
168+
//}
176169

177-
var c = new List<String[]> {a, b};
178-
IEnumerable<string> d = c.MergeInnerEnumerable();
170+
//var c = new List<String[]> {a, b};
171+
//IEnumerable<string> d = c.MergeInnerEnumerable();
179172
}
180173

181174
public void Misc()

lab/Z.ExtensionMethods.Lab/Z.ExtensionMethods.Lab.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
<Reference Include="System.Drawing" />
5151
<Reference Include="System.Windows.Forms" />
5252
<Reference Include="System.Xml" />
53-
<Reference Include="Z.ExtensionMethods.WithNamespace">
54-
<HintPath>..\..\..\..\..\..\OneDrive\Documents\Release\Z.ExtensionMethods\Z.ExtensionMethods.WithNamespace\NET40\Z.ExtensionMethods.WithNamespace.dll</HintPath>
55-
</Reference>
5653
</ItemGroup>
5754
<ItemGroup>
5855
<Compile Include="Form1.cs">
@@ -86,6 +83,10 @@
8683
</Compile>
8784
</ItemGroup>
8885
<ItemGroup>
86+
<ProjectReference Include="..\..\src\Z.Core\Z.Core.csproj">
87+
<Project>{02f009f8-720b-4cbd-98ff-f2c33f4441d5}</Project>
88+
<Name>Z.Core</Name>
89+
</ProjectReference>
8990
<ProjectReference Include="..\..\src\Z.Data.SQLite\Z.Data.SQLite.csproj">
9091
<Project>{40cec7db-c8e6-4a24-8de7-9e0edf8708c6}</Project>
9192
<Name>Z.Data.SQLite</Name>

src/Z.Core/System.Object/Convert/ToValueType/Object.ToBooleanOrDefault.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ public static bool ToBooleanOrDefault(this object @this, bool defaultValue)
4242
return defaultValue;
4343
}
4444
}
45+
/// <summary>
46+
/// An object extension method that converts this object to a boolean or default.
47+
/// </summary>
48+
/// <param name="this">The @this to act on.</param>
49+
/// <param name="defaultValue">true to default value.</param>
50+
/// <param name="useDefaultIfNull">true to use default if null.</param>
51+
/// <returns>The given data converted to a bool.</returns>
52+
public static bool ToBooleanOrDefault(this object @this, bool defaultValue, bool useDefaultIfNull)
53+
{
54+
if (useDefaultIfNull && @this == null)
55+
{
56+
return defaultValue;
57+
}
58+
59+
try
60+
{
61+
return Convert.ToBoolean(@this);
62+
}
63+
catch (Exception)
64+
{
65+
return defaultValue;
66+
}
67+
}
4568

4669
/// <summary>
4770
/// 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<bool> defaultValue
6083
return defaultValueFactory();
6184
}
6285
}
86+
87+
/// <summary>
88+
/// An object extension method that converts this object to a boolean or default.
89+
/// </summary>
90+
/// <param name="this">The @this to act on.</param>
91+
/// <param name="defaultValueFactory">The default value factory.</param>
92+
/// <param name="useDefaultIfNull">true to use default if null.</param>
93+
/// <returns>The given data converted to a bool.</returns>
94+
public static bool ToBooleanOrDefault(this object @this, Func<bool> defaultValueFactory, bool useDefaultIfNull)
95+
{
96+
if (useDefaultIfNull && @this == null)
97+
{
98+
return defaultValueFactory();
99+
}
100+
101+
try
102+
{
103+
return Convert.ToBoolean(@this);
104+
}
105+
catch (Exception)
106+
{
107+
return defaultValueFactory();
108+
}
109+
}
63110
}

src/Z.Core/System.Object/Convert/ToValueType/Object.ToByteOrDefault.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ public static byte ToByteOrDefault(this object @this, byte defaultValue)
4343
}
4444
}
4545

46+
/// <summary>An object extension method that converts this object to a byte or default.</summary>
47+
/// <param name="this">The @this to act on.</param>
48+
/// <param name="defaultValue">The default value.</param>
49+
/// <param name="useDefaultIfNull">true to use default if null.</param>
50+
/// <returns>The given data converted to a byte.</returns>
51+
public static byte ToByteOrDefault(this object @this, byte defaultValue, bool useDefaultIfNull)
52+
{
53+
if (useDefaultIfNull && @this == null)
54+
{
55+
return defaultValue;
56+
}
57+
58+
try
59+
{
60+
return Convert.ToByte(@this);
61+
}
62+
catch (Exception)
63+
{
64+
return defaultValue;
65+
}
66+
}
67+
4668
/// <summary>
4769
/// An object extension method that converts this object to a byte or default.
4870
/// </summary>
@@ -60,4 +82,26 @@ public static byte ToByteOrDefault(this object @this, Func<byte> defaultValueFac
6082
return defaultValueFactory();
6183
}
6284
}
85+
86+
/// <summary>An object extension method that converts this object to a byte or default.</summary>
87+
/// <param name="this">The @this to act on.</param>
88+
/// <param name="defaultValueFactory">The default value factory.</param>
89+
/// <param name="useDefaultIfNull">true to use default if null.</param>
90+
/// <returns>The given data converted to a byte.</returns>
91+
public static byte ToByteOrDefault(this object @this, Func<byte> defaultValueFactory, bool useDefaultIfNull)
92+
{
93+
if (useDefaultIfNull && @this == null)
94+
{
95+
return defaultValueFactory();
96+
}
97+
98+
try
99+
{
100+
return Convert.ToByte(@this);
101+
}
102+
catch (Exception)
103+
{
104+
return defaultValueFactory();
105+
}
106+
}
63107
}

src/Z.Core/System.Object/Convert/ToValueType/Object.ToCharOrDefault.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public static char ToCharOrDefault(this object @this, char defaultValue)
4343
}
4444
}
4545

46+
/// <summary>
47+
/// An object extension method that converts this object to a character or default.
48+
/// </summary>
49+
/// <param name="this">The @this to act on.</param>
50+
/// <param name="defaultValue">The default value.</param>
51+
/// <param name="useDefaultIfNull">true to use default if null.</param>
52+
/// <returns>The given data converted to a char.</returns>
53+
public static char ToCharOrDefault(this object @this, char defaultValue, bool useDefaultIfNull)
54+
{
55+
if (useDefaultIfNull && @this == null)
56+
{
57+
return defaultValue;
58+
}
59+
60+
try
61+
{
62+
return Convert.ToChar(@this);
63+
}
64+
catch (Exception)
65+
{
66+
return defaultValue;
67+
}
68+
}
69+
4670
/// <summary>
4771
/// An object extension method that converts this object to a character or default.
4872
/// </summary>
@@ -60,4 +84,28 @@ public static char ToCharOrDefault(this object @this, Func<char> defaultValueFac
6084
return defaultValueFactory();
6185
}
6286
}
87+
88+
/// <summary>
89+
/// An object extension method that converts this object to a character or default.
90+
/// </summary>
91+
/// <param name="this">The @this to act on.</param>
92+
/// <param name="defaultValueFactory">The default value factory.</param>
93+
/// <param name="useDefaultIfNull">true to use default if null.</param>
94+
/// <returns>The given data converted to a char.</returns>
95+
public static char ToCharOrDefault(this object @this, Func<char> defaultValueFactory, bool useDefaultIfNull)
96+
{
97+
if (useDefaultIfNull && @this == null)
98+
{
99+
return defaultValueFactory();
100+
}
101+
102+
try
103+
{
104+
return Convert.ToChar(@this);
105+
}
106+
catch (Exception)
107+
{
108+
return defaultValueFactory();
109+
}
110+
}
63111
}

src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOffSetOrDefault.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, DateTi
4343
}
4444
}
4545

46+
/// <summary>
47+
/// An object extension method that converts this object to a date time off set or default.
48+
/// </summary>
49+
/// <param name="this">The @this to act on.</param>
50+
/// <param name="defaultValue">The default value.</param>
51+
/// <param name="useDefaultIfNull">true to use default if null.</param>
52+
/// <returns>The given data converted to a DateTimeOffset.</returns>
53+
public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, DateTimeOffset defaultValue, bool useDefaultIfNull)
54+
{
55+
if (useDefaultIfNull && @this == null)
56+
{
57+
return defaultValue;
58+
}
59+
60+
try
61+
{
62+
return new DateTimeOffset(Convert.ToDateTime(@this), TimeSpan.Zero);
63+
}
64+
catch (Exception)
65+
{
66+
return defaultValue;
67+
}
68+
}
69+
4670
/// <summary>
4771
/// An object extension method that converts this object to a date time off set or default.
4872
/// </summary>
@@ -60,4 +84,28 @@ public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, Func<D
6084
return defaultValueFactory();
6185
}
6286
}
87+
88+
/// <summary>
89+
/// An object extension method that converts this object to a date time off set or default.
90+
/// </summary>
91+
/// <param name="this">The @this to act on.</param>
92+
/// <param name="defaultValueFactory">The default value factory.</param>
93+
/// <param name="useDefaultIfNull">true to use default if null.</param>
94+
/// <returns>The given data converted to a DateTimeOffset.</returns>
95+
public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, Func<DateTimeOffset> defaultValueFactory, bool useDefaultIfNull)
96+
{
97+
if (useDefaultIfNull && @this == null)
98+
{
99+
return defaultValueFactory();
100+
}
101+
102+
try
103+
{
104+
return new DateTimeOffset(Convert.ToDateTime(@this), TimeSpan.Zero);
105+
}
106+
catch (Exception)
107+
{
108+
return defaultValueFactory();
109+
}
110+
}
63111
}

src/Z.Core/System.Object/Convert/ToValueType/Object.ToDateTimeOrDefault.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public static DateTime ToDateTimeOrDefault(this object @this, DateTime defaultVa
4343
}
4444
}
4545

46+
/// <summary>
47+
/// An object extension method that converts this object to a date time or default.
48+
/// </summary>
49+
/// <param name="this">The @this to act on.</param>
50+
/// <param name="defaultValue">The default value.</param>
51+
/// <param name="useDefaultIfNull">true to use default if null.</param>
52+
/// <returns>The given data converted to a DateTime.</returns>
53+
public static DateTime ToDateTimeOrDefault(this object @this, DateTime defaultValue, bool useDefaultIfNull)
54+
{
55+
if (useDefaultIfNull && @this == null)
56+
{
57+
return defaultValue;
58+
}
59+
60+
try
61+
{
62+
return Convert.ToDateTime(@this);
63+
}
64+
catch (Exception)
65+
{
66+
return defaultValue;
67+
}
68+
}
69+
4670
/// <summary>
4771
/// An object extension method that converts this object to a date time or default.
4872
/// </summary>
@@ -60,4 +84,28 @@ public static DateTime ToDateTimeOrDefault(this object @this, Func<DateTime> def
6084
return defaultValueFactory();
6185
}
6286
}
87+
88+
/// <summary>
89+
/// An object extension method that converts this object to a date time or default.
90+
/// </summary>
91+
/// <param name="this">The @this to act on.</param>
92+
/// <param name="defaultValueFactory">The default value factory.</param>
93+
/// <param name="useDefaultIfNull">true to use default if null.</param>
94+
/// <returns>The given data converted to a DateTime.</returns>
95+
public static DateTime ToDateTimeOrDefault(this object @this, Func<DateTime> defaultValueFactory, bool useDefaultIfNull)
96+
{
97+
if (useDefaultIfNull && @this == null)
98+
{
99+
return defaultValueFactory();
100+
}
101+
102+
try
103+
{
104+
return Convert.ToDateTime(@this);
105+
}
106+
catch (Exception)
107+
{
108+
return defaultValueFactory();
109+
}
110+
}
63111
}

0 commit comments

Comments
 (0)