[英]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. 您对基本类型的看法有多种可能的解释。
.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. 请注意,
IntPtr
和UIntPtr
也在那里。 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不会将
String
或Decimal
视为基元。
You can test it like this: 你可以像这样测试它:
public static bool IsPrimitiveType(Type type)
{
return type.IsPrimitive;
}
In your question you have included the String
and Decimal
types in your definition of a primitive type. 在您的问题中,您在基本类型的定义中包含了
String
和Decimal
类型。 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. 由于无法扩展
String
或Decimal
,因此简单类型相等就足够了。
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#参考)列表,我们必须排除
IntPtr
和UIntPtr
因为它们不在该列表中。
public static bool IsPrimitiveType(Type type)
{
return (type.IsPrimitive
&& type != typeof(UIntPtr)
&& type != typeof(IntPtr))
|| type == typeof(decimal)
|| type == typeof(string);
}
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
方法:
If you have an object instance obj
: 如果你有一个对象实例
obj
:
bool isPrimitive = IsPrimitiveType(obj.GetType());
If you have a type someType
: 如果你有一个
someType
类型:
bool isPrimitive = IsPrimitiveType(someType);
If you have a generic type parameter T
: 如果您有一个泛型类型参数
T
:
bool isPrimitive = IsPrimitiveType(typeof(T));
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.