简体   繁体   中英

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...)?

EDIT

As a "base type" I mean all primitive types known to the 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#.

So we can use the Built-In Types Table (C# Reference) to deduce:

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.

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.

There are multiple possible interpretations of what you think of as primitive types.

1. .NET primitive types

.NET has a list of types that it considers to be primitive types. 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.

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

Note that IntPtr and UIntPtr are also in there. They represent the platform-specific integer type (eg 32-bit integer on a 32-bit computer, 64-bit on a 64-bit computer). Also note that .NET does not consider String or Decimal to be a primitive.

You can test it like this:

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

2. .NET primitive types and String and Decimal

In your question you have included the String and Decimal types in your definition of a primitive type. 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.

3. Built-in C# types

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.

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:

  1. If you have an object instance obj :

     bool isPrimitive = IsPrimitiveType(obj.GetType()); 
  2. If you have a type someType :

     bool isPrimitive = IsPrimitiveType(someType); 
  3. If you have a generic type parameter T :

     bool isPrimitive = IsPrimitiveType(typeof(T)); 
  4. If you have a type known at compile time, eg 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;

Hope can help you think through it

Use instanceof operator

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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