C#中判斷某類型是否可以進行隱式類型轉換
更新時間:2013年04月26日 15:16:19 作者:
在我們采用反射動態(tài)調用一些方法時,常常涉及到類型的轉換,直接判斷類型是否相符有時不能判斷調用方法是否合適
C#中,有些類型是可以隱式轉換的,我整理了這些可以隱式轉換的類型,供大家參考
復制代碼 代碼如下:
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# 中 Array和 ArrayList詳解及區(qū)別
這篇文章主要介紹了C# 中 Array和 ArrayList詳解及區(qū)別的相關資料,需要的朋友可以參考下2017-01-01
在winform下實現左右布局多窗口界面的方法之續(xù)篇
這篇文章主要介紹了在winform下實現左右布局多窗口界面的方法之續(xù)篇 的相關資料,需要的朋友可以參考下2016-02-02
C#實現主窗體最小化后出現懸浮框及雙擊懸浮框恢復原窗體的方法
這篇文章主要介紹了C#實現主窗體最小化后出現懸浮框及雙擊懸浮框恢復原窗體的方法,涉及C#窗體及鼠標事件響應的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08
Unity?Shader編輯器工具類ShaderUtil?常用函數和用法實例詳解
Unity的Shader編輯器工具類ShaderUtil提供了一系列函數,用于編譯、導入和管理著色器,這篇文章主要介紹了Unity?Shader編輯器工具類ShaderUtil?常用函數和用法,需要的朋友可以參考下2023-08-08
Unity編輯器資源導入處理函數OnPostprocessTexture實例深入解析
這篇文章主要為大家介紹了Unity編輯器資源導入處理函數OnPostprocessTexture實例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09

