簡體   English   中英

如何確保傳入的對象是基類型(String,Int16,Int32,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語句。

您對基本類型的看法有多種可能的解釋。

1. .NET原始類型

.NET有一個它認為是原始類型的類型列表。 Type類上有一個屬性IsPrimitive屬性 ,它將對任何這些基元類型返回true ,對任何其他類型返回false

基元類型是布爾,字節,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,IntPtr,UIntPtr,Char,Double和Single。

請注意, IntPtrUIntPtr也在那里。 它們表示特定於平台的整數類型(例如,32位計算機上的32位整數,64位計算機上的64位整數)。 另請注意,.NET不會將StringDecimal視為基元。

你可以像這樣測試它:

public static bool IsPrimitiveType(Type type)
{
    return type.IsPrimitive;
}

2. .NET原始類型和String和Decimal

在您的問題中,您在基本類型的定義中包含了StringDecimal類型。 讓我們測試一下,像這樣:

public static bool IsPrimitiveType(Type type)
{
    return type.IsPrimitive
        || type == typeof(decimal)
        || type == typeof(string);
}

由於無法擴展StringDecimal ,因此簡單類型相等就足夠了。

3.內置C#類型

如果原始類型的定義是MSDN上的內置類型表(C#參考)列表,我們必須排除IntPtrUIntPtr因為它們不在該列表中。

public static bool IsPrimitiveType(Type type)
{
    return (type.IsPrimitive
         && type != typeof(UIntPtr)
         && type != typeof(IntPtr))
        || type == typeof(decimal)
        || type == typeof(string);
}

完全是另一回事

根據前面的示例,您可以了解如何在基本類型的定義中排除或包含其他類型(如果需要)。


在上面的所有示例中,您可以像這樣調用IsPrimitiveType方法:

  1. 如果你有一個對象實例obj

     bool isPrimitive = IsPrimitiveType(obj.GetType()); 
  2. 如果你有一個someType類型:

     bool isPrimitive = IsPrimitiveType(someType); 
  3. 如果您有一個泛型類型參數T

     bool isPrimitive = IsPrimitiveType(typeof(T)); 
  4. 如果您在編譯時已知類型,例如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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM