Skip to content

Commit

Permalink
Add overload useDefaultIfNull
Browse files Browse the repository at this point in the history
Add overload useDefaultIfNull
  • Loading branch information
zzzprojects committed Jun 28, 2017
1 parent 2411aa4 commit f6fd4ed
Show file tree
Hide file tree
Showing 24 changed files with 1,061 additions and 20 deletions.
27 changes: 10 additions & 17 deletions lab/Z.ExtensionMethods.Lab/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "a<b class='to>to'>c</b>d";
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<String[]> {a, b};
IEnumerable<string> d = c.MergeInnerEnumerable();
//var c = new List<String[]> {a, b};
//IEnumerable<string> d = c.MergeInnerEnumerable();
}

public void Misc()
Expand Down
7 changes: 4 additions & 3 deletions lab/Z.ExtensionMethods.Lab/Z.ExtensionMethods.Lab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Z.ExtensionMethods.WithNamespace">
<HintPath>..\..\..\..\..\..\OneDrive\Documents\Release\Z.ExtensionMethods\Z.ExtensionMethods.WithNamespace\NET40\Z.ExtensionMethods.WithNamespace.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
Expand Down Expand Up @@ -86,6 +83,10 @@
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Z.Core\Z.Core.csproj">
<Project>{02f009f8-720b-4cbd-98ff-f2c33f4441d5}</Project>
<Name>Z.Core</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Z.Data.SQLite\Z.Data.SQLite.csproj">
<Project>{40cec7db-c8e6-4a24-8de7-9e0edf8708c6}</Project>
<Name>Z.Data.SQLite</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ public static bool ToBooleanOrDefault(this object @this, bool defaultValue)
return defaultValue;
}
}
/// <summary>
/// An object extension method that converts this object to a boolean or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValue">true to default value.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a bool.</returns>
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;
}
}

/// <summary>
/// An object extension method that converts this object to a boolean or default.
Expand All @@ -60,4 +83,28 @@ public static bool ToBooleanOrDefault(this object @this, Func<bool> defaultValue
return defaultValueFactory();
}
}

/// <summary>
/// An object extension method that converts this object to a boolean or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValueFactory">The default value factory.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a bool.</returns>
public static bool ToBooleanOrDefault(this object @this, Func<bool> defaultValueFactory, bool useDefaultIfNull)
{
if (useDefaultIfNull && @this == null)
{
return defaultValueFactory();
}

try
{
return Convert.ToBoolean(@this);
}
catch (Exception)
{
return defaultValueFactory();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ public static byte ToByteOrDefault(this object @this, byte defaultValue)
}
}

/// <summary>An object extension method that converts this object to a byte or default.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a byte.</returns>
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;
}
}

/// <summary>
/// An object extension method that converts this object to a byte or default.
/// </summary>
Expand All @@ -60,4 +82,26 @@ public static byte ToByteOrDefault(this object @this, Func<byte> defaultValueFac
return defaultValueFactory();
}
}

/// <summary>An object extension method that converts this object to a byte or default.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValueFactory">The default value factory.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a byte.</returns>
public static byte ToByteOrDefault(this object @this, Func<byte> defaultValueFactory, bool useDefaultIfNull)
{
if (useDefaultIfNull && @this == null)
{
return defaultValueFactory();
}

try
{
return Convert.ToByte(@this);
}
catch (Exception)
{
return defaultValueFactory();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public static char ToCharOrDefault(this object @this, char defaultValue)
}
}

/// <summary>
/// An object extension method that converts this object to a character or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a char.</returns>
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;
}
}

/// <summary>
/// An object extension method that converts this object to a character or default.
/// </summary>
Expand All @@ -60,4 +84,28 @@ public static char ToCharOrDefault(this object @this, Func<char> defaultValueFac
return defaultValueFactory();
}
}

/// <summary>
/// An object extension method that converts this object to a character or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValueFactory">The default value factory.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a char.</returns>
public static char ToCharOrDefault(this object @this, Func<char> defaultValueFactory, bool useDefaultIfNull)
{
if (useDefaultIfNull && @this == null)
{
return defaultValueFactory();
}

try
{
return Convert.ToChar(@this);
}
catch (Exception)
{
return defaultValueFactory();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, DateTi
}
}

/// <summary>
/// An object extension method that converts this object to a date time off set or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a DateTimeOffset.</returns>
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;
}
}

/// <summary>
/// An object extension method that converts this object to a date time off set or default.
/// </summary>
Expand All @@ -60,4 +84,28 @@ public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, Func<D
return defaultValueFactory();
}
}

/// <summary>
/// An object extension method that converts this object to a date time off set or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValueFactory">The default value factory.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a DateTimeOffset.</returns>
public static DateTimeOffset ToDateTimeOffSetOrDefault(this object @this, Func<DateTimeOffset> defaultValueFactory, bool useDefaultIfNull)
{
if (useDefaultIfNull && @this == null)
{
return defaultValueFactory();
}

try
{
return new DateTimeOffset(Convert.ToDateTime(@this), TimeSpan.Zero);
}
catch (Exception)
{
return defaultValueFactory();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public static DateTime ToDateTimeOrDefault(this object @this, DateTime defaultVa
}
}

/// <summary>
/// An object extension method that converts this object to a date time or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValue">The default value.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a DateTime.</returns>
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;
}
}

/// <summary>
/// An object extension method that converts this object to a date time or default.
/// </summary>
Expand All @@ -60,4 +84,28 @@ public static DateTime ToDateTimeOrDefault(this object @this, Func<DateTime> def
return defaultValueFactory();
}
}

/// <summary>
/// An object extension method that converts this object to a date time or default.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="defaultValueFactory">The default value factory.</param>
/// <param name="useDefaultIfNull">true to use default if null.</param>
/// <returns>The given data converted to a DateTime.</returns>
public static DateTime ToDateTimeOrDefault(this object @this, Func<DateTime> defaultValueFactory, bool useDefaultIfNull)
{
if (useDefaultIfNull && @this == null)
{
return defaultValueFactory();
}

try
{
return Convert.ToDateTime(@this);
}
catch (Exception)
{
return defaultValueFactory();
}
}
}
Loading

0 comments on commit f6fd4ed

Please sign in to comment.