简体   繁体   English

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

[英]How to make sure that incoming object is a base type (String, Int16, Int32, Double…)?

So I have the method: 所以我有方法:

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);
}

How to make sure that this incoming obj is a base type (String, Int32, Double, Int16, Decimal...)? 如何确保这个传入的obj是一个基类型(String,Int32,Double,Int16,Decimal ......)?

EDIT 编辑

As a "base type" I mean all primitive types known to the C#. 作为“基类型”,我指的是C#已知的所有原始类型。

There is no automatic list of "built in" types in the runtime, since different languages can have different built-in support for types. 由于不同的语言可以对类型具有不同的内置支持,因此运行时中没有“内置”类型的自动列表。

As a "base type" i mean all primitive types known to the C#. 作为“基类型”,我指的是C#已知的所有原始类型。

So we can use the Built-In Types Table (C# Reference) to deduce: 因此我们可以使用内置类型表(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;
}

Note I omitted object , for hopefully obvious reasons. 注意我省略了object ,希望显而易见的原因。

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

Seems everyone is doing it really complicated, with long lists of conditions or big switch statements. 似乎每个人都做得很复杂,有很长的条件列表或大的switch语句。

There are multiple possible interpretations of what you think of as primitive types. 您对基本类型的看法有多种可能的解释。

1. .NET primitive types 1. .NET原始类型

.NET has a list of types that it considers to be primitive types. .NET有一个它认为是原始类型的类型列表。 On the Type class there is a property IsPrimitive property that will return true for any of these primitive types and false for any other type. Type类上有一个属性IsPrimitive属性 ,它将对任何这些基元类型返回true ,对任何其他类型返回false

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. 基元类型是布尔,字节,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,IntPtr,UIntPtr,Char,Double和Single。

Note that IntPtr and UIntPtr are also in there. 请注意, IntPtrUIntPtr也在那里。 They represent the platform-specific integer type (eg 32-bit integer on a 32-bit computer, 64-bit on a 64-bit computer). 它们表示特定于平台的整数类型(例如,32位计算机上的32位整数,64位计算机上的64位整数)。 Also note that .NET does not consider String or Decimal to be a primitive. 另请注意,.NET不会将StringDecimal视为基元。

You can test it like this: 你可以像这样测试它:

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

2. .NET primitive types and String and Decimal 2. .NET原始类型和String和Decimal

In your question you have included the String and Decimal types in your definition of a primitive type. 在您的问题中,您在基本类型的定义中包含了StringDecimal类型。 Let's test for those too, like this: 让我们测试一下,像这样:

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

Since it is impossible to extend String or Decimal , simple type equality will suffice here. 由于无法扩展StringDecimal ,因此简单类型相等就足够了。

3. Built-in C# types 3.内置C#类型

If your definition of primitive types is the list of Built-in Types Table (C# Reference) on MSDN, we have to exclude IntPtr and UIntPtr because they are not in that list. 如果原始类型的定义是MSDN上的内置类型表(C#参考)列表,我们必须排除IntPtrUIntPtr因为它们不在该列表中。

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

4. Something else entirely 完全是另一回事

Based on the previous examples you can see how to exclude or include additional types in your definition of a primitive type if you want to. 根据前面的示例,您可以了解如何在基本类型的定义中排除或包含其他类型(如果需要)。


In all the above examples, you can call the IsPrimitiveType method like this: 在上面的所有示例中,您可以像这样调用IsPrimitiveType方法:

  1. If you have an object instance obj : 如果你有一个对象实例obj

     bool isPrimitive = IsPrimitiveType(obj.GetType()); 
  2. If you have a type someType : 如果你有一个someType类型:

     bool isPrimitive = IsPrimitiveType(someType); 
  3. If you have a generic type parameter T : 如果您有一个泛型类型参数T

     bool isPrimitive = IsPrimitiveType(typeof(T)); 
  4. If you have a type known at compile time, eg Int32 : 如果您在编译时已知类型,例如Int32

     bool isPrimitive = IsPrimitiveType(typeof(Int32)); 

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

Hope can help you think through it 希望可以帮助你思考它

Use instanceof operator 使用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";

the above code will throw an assertion error is the type is not a primitive type or null. 上面的代码将抛出一个断言错误,类型不是基本类型或null。

So I get how to do it! 所以我明白了怎么做!

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

This will do what I wanted. 这将做我想要的。 Thx a lot to the people who tried to help me! 对那些试图帮助我的人来说很重要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM