![](/img/trans.png)
[英]Typecast any OleDbReader Double/Int16/Int32/Int64 to C# double?
[英]How to make sure that incoming object is a base type (String, Int16, Int32, Double…)?
所以我有方法:
public Boolean IsItABaseType(object obj)
{
// How to make sure that this incoming obj
// is a base type (String, Int32, Double, Int16, Decimal...).
Boolean isBaseType = obj...
Console.WriteLine(typeof(obj).Name);
Console.WriteLIne("obj is base type"+isBaseType);
}
如何確保這個傳入的obj是一個基類型(String,Int32,Double,Int16,Decimal ......)?
編輯
作為“基類型”,我指的是C#已知的所有原始類型。
由於不同的語言可以對類型具有不同的內置支持,因此運行時中沒有“內置”類型的自動列表。
作為“基類型”,我指的是C#已知的所有原始類型。
因此我們可以使用內置類型表(C#參考)來推斷:
switch(Type.GetTypeCode(obj.GetType()) {
case TypeCode.Boolean:
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Char:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.String:
// do stuff for "built in" types
...
break;
default:
// do stuff for all other types
...
break;
}
注意我省略了object
,希望顯而易見的原因。
bool isBaseType = obj is string || obj is int || obj is double || obj is decimal ...;
似乎每個人都做得很復雜,有很長的條件列表或大的switch
語句。
您對基本類型的看法有多種可能的解釋。
.NET有一個它認為是原始類型的類型列表。 在Type
類上有一個屬性IsPrimitive
屬性 ,它將對任何這些基元類型返回true
,對任何其他類型返回false
。
基元類型是布爾,字節,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,IntPtr,UIntPtr,Char,Double和Single。
請注意, IntPtr
和UIntPtr
也在那里。 它們表示特定於平台的整數類型(例如,32位計算機上的32位整數,64位計算機上的64位整數)。 另請注意,.NET不會將String
或Decimal
視為基元。
你可以像這樣測試它:
public static bool IsPrimitiveType(Type type)
{
return type.IsPrimitive;
}
在您的問題中,您在基本類型的定義中包含了String
和Decimal
類型。 讓我們測試一下,像這樣:
public static bool IsPrimitiveType(Type type)
{
return type.IsPrimitive
|| type == typeof(decimal)
|| type == typeof(string);
}
由於無法擴展String
或Decimal
,因此簡單類型相等就足夠了。
如果原始類型的定義是MSDN上的內置類型表(C#參考)列表,我們必須排除IntPtr
和UIntPtr
因為它們不在該列表中。
public static bool IsPrimitiveType(Type type)
{
return (type.IsPrimitive
&& type != typeof(UIntPtr)
&& type != typeof(IntPtr))
|| type == typeof(decimal)
|| type == typeof(string);
}
根據前面的示例,您可以了解如何在基本類型的定義中排除或包含其他類型(如果需要)。
在上面的所有示例中,您可以像這樣調用IsPrimitiveType
方法:
如果你有一個對象實例obj
:
bool isPrimitive = IsPrimitiveType(obj.GetType());
如果你有一個someType
類型:
bool isPrimitive = IsPrimitiveType(someType);
如果您有一個泛型類型參數T
:
bool isPrimitive = IsPrimitiveType(typeof(T));
如果您在編譯時已知類型,例如Int32
:
bool isPrimitive = IsPrimitiveType(typeof(Int32));
你可以..... if (obj == typeof(double)) blablblabl; if (obj == typeof(int)) blablablaaa2;
if (obj == typeof(double)) blablblabl; if (obj == typeof(int)) blablablaaa2;
希望可以幫助你思考它
使用instanceof運算符
assert (obj instanceof Integer || obj instanceof Boolean|| obj instanceof String|| obj instanceof Double|| obj instanceof Short|| obj instanceof Long|| obj instanceof Float|| obj instanceof Chracter) : "input is not a valid datatype";
上面的代碼將拋出一個斷言錯誤,類型不是基本類型或null。
所以我明白了怎么做!
var t = obj.GetType();
Boolean isInSystemNameSpace = t.Namespace == "System";
var result = t == typeof(string) || t.IsValueType && isInSystemNameSpace;
這將做我想要的。 對那些試圖幫助我的人來說很重要!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.