188 lines
4.2 KiB
C#
188 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace GSYWApi
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class TypeParseExtenions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static bool IsEmpty(this object[] me)
|
|
{
|
|
if (me == null) return true;
|
|
|
|
if (me.Length == 0) return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static bool IsNullOrDBNull(this object me)
|
|
{
|
|
return !(me != null && me != DBNull.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static T As<T>(this object me)
|
|
{
|
|
return As<T>(me, default(T));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static T As<T>(this object me, T defaultValue)
|
|
{
|
|
T v;
|
|
try
|
|
{
|
|
v = (T)Convert.ChangeType(me, typeof(T));
|
|
}
|
|
catch
|
|
{
|
|
v = defaultValue;
|
|
}
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static DateTime? TryParseToDateTime(this object me)
|
|
{
|
|
if (me.IsNullOrDBNull())
|
|
{
|
|
return null;
|
|
}
|
|
else if (me is DateTime)
|
|
{
|
|
return (DateTime)me;
|
|
}
|
|
else
|
|
{
|
|
DateTime v;
|
|
if (DateTime.TryParse(me.ToString(), out v))
|
|
{
|
|
return v;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static string TryParseToString(this object me)
|
|
{
|
|
return me == null ? string.Empty : Convert.ToString(me);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static bool TryParseToBool(this object me, bool defaultValue = false)
|
|
{
|
|
if (me.IsNullOrDBNull())
|
|
return defaultValue;
|
|
if (me is int)
|
|
{
|
|
return (int)me > 0 ? true : false;
|
|
}
|
|
else
|
|
{
|
|
bool v;
|
|
bool.TryParse(me.ToString(), out v);
|
|
return v;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static short? TryParseToShort(this object me, short? defaultValue = null)
|
|
{
|
|
short? v = defaultValue;
|
|
try
|
|
{
|
|
v = short.Parse(decimal.Parse(me.ToString()).ToString("0"));
|
|
}
|
|
catch { }
|
|
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static int? TryParseToInt(this object me, int? defaultValue = null)
|
|
{
|
|
int? v = defaultValue;
|
|
try
|
|
{
|
|
v = int.Parse(decimal.Parse(me.ToString()).ToString("0"));
|
|
}
|
|
catch { }
|
|
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static long? TryParseToLong(this object me, long? defaultValue = null)
|
|
{
|
|
long? v = defaultValue;
|
|
try
|
|
{
|
|
v = long.Parse(decimal.Parse(me.ToString()).ToString("0"));
|
|
}
|
|
catch { }
|
|
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static decimal? TryParseToDecimal(this object me, decimal? defaultValue = null)
|
|
{
|
|
decimal? v = defaultValue;
|
|
try
|
|
{
|
|
v = decimal.Parse(me.ToString());
|
|
}
|
|
catch { }
|
|
|
|
return v;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static double? TryParseToDouble(this object me, double? defaultValue = null)
|
|
{
|
|
double? v = defaultValue;
|
|
try
|
|
{
|
|
v = double.Parse(me.ToString());
|
|
}
|
|
catch { }
|
|
|
|
return v;
|
|
}
|
|
}
|
|
}
|