如何确保传入的对象是基类型(String,Int16,Int32,Double ...)?

时间:2013-03-15 07:39:03

标签: c# .net reflection

所以我有方法:

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#已知的所有原始类型。

6 个答案:

答案 0 :(得分:7)

没有自动列表"内置"运行时中的类型,因为不同的语言可以对类型具有不同的内置支持。

  

作为"基础类型"我指的是C#已知的所有原始类型。

因此我们可以使用Built-In Types Table (C# Reference)来推断:

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,原因很明显。

答案 1 :(得分:3)

bool isBaseType = obj is string || obj is int || obj is double || obj is decimal ...;

答案 2 :(得分:3)

似乎每个人都做得非常复杂,有很长的条件列表或大switch个陈述。

您对基本类型的看法有多种可能的解释。

1。 .NET原始类型

.NET有一个它认为是基本类型的类型列表。在Type class上有一个属性IsPrimitive property,对于任何这些原始类型都会返回true,对于任何其他类型都会返回false

  

基本类型包括Boolean,Byte,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上Built-in Types Table (C# Reference)的列表,我们必须排除IntPtrUIntPtr,因为它们不在该列表中。

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

4。其他完全

根据前面的示例,您可以了解如何在基元类型的定义中排除或包含其他类型。


在上面的所有示例中,您可以像这样调用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));
    

答案 3 :(得分:0)

你可以...... if (obj == typeof(double)) blablblabl; if (obj == typeof(int)) blablablaaa2;

希望可以帮助你思考

答案 4 :(得分:0)

使用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。

答案 5 :(得分:0)

所以我知道怎么做!

    var t = obj.GetType();
    Boolean isInSystemNameSpace = t.Namespace == "System";
    var result = t == typeof(string) || t.IsValueType && isInSystemNameSpace; 

这将做我想要的。对那些试图帮助我的人来说很重要!