Mar*_*ris 2 .net c# 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);
}
Run Code Online (Sandbox Code Playgroud)
如何确保这个传入的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;
}
Run Code Online (Sandbox Code Playgroud)
注意我省略了object,原因很明显.
| 归档时间: |
|
| 查看次数: |
525 次 |
| 最近记录: |