C#中判斷某類型是否可以進(jìn)行隱式類型轉(zhuǎn)換
C#中,有些類型是可以隱式轉(zhuǎn)換的,我整理了這些可以隱式轉(zhuǎn)換的類型,供大家參考
static private bool CanConvert(Type from, Type to)
{
if (from.IsPrimitive && to.IsPrimitive)
{
TypeCode typeCodeFrom = Type.GetTypeCode(from);
TypeCode typeCodeTo = Type.GetTypeCode(to);
if (typeCodeFrom == typeCodeTo)
return true;
if (typeCodeFrom == TypeCode.Char)
switch (typeCodeTo)
{
case TypeCode.UInt16: return true;
case TypeCode.UInt32: return true;
case TypeCode.Int32: return true;
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Byte follow.
if (typeCodeFrom == TypeCode.Byte)
switch (typeCodeTo)
{
case TypeCode.Char: return true;
case TypeCode.UInt16: return true;
case TypeCode.Int16: return true;
case TypeCode.UInt32: return true;
case TypeCode.Int32: return true;
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from SByte follow.
if (typeCodeFrom == TypeCode.SByte)
switch (typeCodeTo)
{
case TypeCode.Int16: return true;
case TypeCode.Int32: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from UInt16 follow.
if (typeCodeFrom == TypeCode.UInt16)
switch (typeCodeTo)
{
case TypeCode.UInt32: return true;
case TypeCode.Int32: return true;
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Int16 follow.
if (typeCodeFrom == TypeCode.Int16)
switch (typeCodeTo)
{
case TypeCode.Int32: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from UInt32 follow.
if (typeCodeFrom == TypeCode.UInt32)
switch (typeCodeTo)
{
case TypeCode.UInt64: return true;
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Int32 follow.
if (typeCodeFrom == TypeCode.Int32)
switch (typeCodeTo)
{
case TypeCode.Int64: return true;
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from UInt64 follow.
if (typeCodeFrom == TypeCode.UInt64)
switch (typeCodeTo)
{
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Int64 follow.
if (typeCodeFrom == TypeCode.Int64)
switch (typeCodeTo)
{
case TypeCode.Single: return true;
case TypeCode.Double: return true;
default: return false;
}
// Possible conversions from Single follow.
if (typeCodeFrom == TypeCode.Single)
switch (typeCodeTo)
{
case TypeCode.Double: return true;
default: return false;
}
}
return false;
}
- C#強(qiáng)制類型轉(zhuǎn)換小結(jié)
- C#將布爾類型轉(zhuǎn)換成字節(jié)數(shù)組的方法
- C#匿名方法與Delegate類型轉(zhuǎn)換錯(cuò)誤分析
- C#引用類型轉(zhuǎn)換的常見(jiàn)方式總結(jié)
- c#入門之類型轉(zhuǎn)換詳解
- c#之用戶定義的數(shù)據(jù)類型轉(zhuǎn)換介紹
- C#用戶定義類型轉(zhuǎn)換詳解
- c#將list類型轉(zhuǎn)換成DataTable方法示例
- 淺析C#數(shù)據(jù)類型轉(zhuǎn)換的幾種形式
- C#自動(dòng)類型轉(zhuǎn)換與強(qiáng)制類型轉(zhuǎn)換的講解
相關(guān)文章
C#連接數(shù)據(jù)庫(kù)和更新數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#連接數(shù)據(jù)庫(kù)和更新數(shù)據(jù)庫(kù)的方法,需要的朋友可以參考下2015-08-08
C# 中 Array和 ArrayList詳解及區(qū)別
這篇文章主要介紹了C# 中 Array和 ArrayList詳解及區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-01-01
在winform下實(shí)現(xiàn)左右布局多窗口界面的方法之續(xù)篇
這篇文章主要介紹了在winform下實(shí)現(xiàn)左右布局多窗口界面的方法之續(xù)篇 的相關(guān)資料,需要的朋友可以參考下2016-02-02
C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了順序表的定義、原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方法
這篇文章主要介紹了C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方法,涉及C#窗體及鼠標(biāo)事件響應(yīng)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
Unity?Shader編輯器工具類ShaderUtil?常用函數(shù)和用法實(shí)例詳解
Unity的Shader編輯器工具類ShaderUtil提供了一系列函數(shù),用于編譯、導(dǎo)入和管理著色器,這篇文章主要介紹了Unity?Shader編輯器工具類ShaderUtil?常用函數(shù)和用法,需要的朋友可以參考下2023-08-08
winform壁紙工具為圖片添加當(dāng)前月的日歷信息
使用用winform做了一個(gè)設(shè)置壁紙小工具,為圖片添加當(dāng)月的日歷并設(shè)為壁紙,可以手動(dòng)/定時(shí)設(shè)置壁紙,最主要的特點(diǎn)是在圖片上生成當(dāng)前月的日歷信息,感興趣的你可以參考下2013-03-03
Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析
這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

