即使对象的当前值为空,是否有更好的方法来确定对象的基础类型?
null
当 someObject 确实是字符串类型但当前持有一个值时,以下代码不起作用。
public void doWork(object someObject)
{
var objAsString = someObject as string;
if (objAsString != null)
{
// do work, return
// work involves string specific logging/manupulation
}
var objAsByteArr = someObject as byte[];
if (objAsByteArr != null)
{
// do work, return
// work involves byte specific logging/manupulation
}
throw new Exception("Unknown type encountered");
}