<T>として単純型変換のC#

システムを使用しました。
System.Collections.Genericを使用しました。
System.Dataを使用しました。
System.Linqのを使用しました。
System.Webのを使用して、

名前空間Uilt
{

//クラスを返し
パブリック静的クラスはObjectHelper
{
//単純型変換
としてT <T>のパブリックstatic(この値は、TはdefaultValue =デフォルト(T)オブジェクト)
{
T RES =はdefaultValueを、
IF(値== NULL)//れますデフォルトまたはヌル値
{
戻りRES;
}
オブジェクトresValue = NULL;
タイプvalue.GetType fromType =();
タイプそのtoType = typeof演算(T)
のTypeCode fromTypeCode = Type.GetTypeCode(fromType); //原点タイプ
のTypeCode toTypeCode =タイプ。 GetTypeCode(toType); //結果の型
のtry
{
スイッチ(toTypeCode)
{

ケースTypeCode.Boolean:
#regionブール値转换
スイッチ(fromTypeCode)
{
ケースTypeCode.SByte:
resValue =(sbyte)値> 0。
ブレーク;
ケースTypeCode.Byte:
resValue =(バイト)値> 0。
ブレーク;
ケースTypeCode.Int16:
resValue =(ショート)値> 0。
ブレーク;
ケースTypeCode.UInt16:
resValue =(USHORT)値> 0。
ブレーク;
ケースTypeCode.Int32:
resValue =(INT)値> 0。
ブレーク;
ケースTypeCode.UInt32:
!resValue =(UINT)値= 0;
ブレーク;
ケースTypeCode.Int64:
resValue =(長い)値> 0L。
ブレーク;
ケースTypeCode.UInt64:
resValue =(ULONG)値> 0L。
ブレーク;
ケースTypeCode.String:
試し
{
resValue = int.Parse((文字列)値)> 0。
}
キャッチ
{
}
ブレーク。
}
resValue =((IConvertible)値).ToBoolean(NULL)。
#endregionの
休憩。
ケースTypeCode.Char:
resValue =((IConvertible)値).ToChar(NULL)。
ブレーク;
ケースTypeCode.SByte:
resValue =((IConvertible)値).ToSByte(NULL)。

ブレーク;
ケースTypeCode.Byte:
resValue =((IConvertible)値).ToByte(NULL)。

ブレーク;
ケースTypeCode.Int16:
resValue =((IConvertible)値).ToInt16(NULL)。
ブレーク;
ケースTypeCode.UInt16:
resValue =((IConvertible)値).ToUInt16(NULL)。
ブレーク;
ケースTypeCode.Int32:
resValue =((IConvertible)値).ToInt32(NULL)。
ブレーク;
ケースTypeCode.UInt32:
resValue =((IConvertible)値).ToUInt32(NULL)。

ブレーク;
ケースTypeCode.Int64:
resValue =((IConvertible)値).ToInt64(NULL)。

ブレーク;
ケースTypeCode.UInt64:
resValue =((IConvertible)値).ToUInt64(NULL)。

ブレーク;
ケースTypeCode.Single:
resValue =((IConvertible)値).ToSingle(NULL)。

ブレーク;
ケースTypeCode.Double:
resValue =((IConvertible)値).ToDouble(NULL)。
ブレーク;
ケースTypeCode.Decimal:
resValue =((IConvertible)値).ToDecimal(NULL)。
ブレーク;
ケースTypeCode.DateTime:
resValue =((IConvertible)値).ToDateTime(NULL)。
ブレーク;
ケースTypeCode.String:
resValue =(値== NULL String.Emptyを:((IConvertible)値).ToString(NULL))。
ブレーク;
デフォルト:
resValue =値;
ブレーク;
}


IF(!resValue = NULL)
{
RES =(T)resValue。
}
}
キャッチ(例外)
{

}
RESを返します。
}
}
}

おすすめ

転載: www.cnblogs.com/Wilson6/p/12147292.html