繁体   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