检查对象是否可以转换为特定值类型?

Max*_*kov 2 c#

我有一个Hashtable,其中包含解析某个JSON的结果:decodedJson.decodedJson["key"]可以是int,double,float,decimal或string.我需要将它转换为十进制,如果它是一个数字(我计划用它(decimal)decodedJson["key"]),或者如果不是则处理错误.

什么是最有效的方法来确定?

Ana*_*ion 5

if (decodedJson["key"] is decimal)
{
//do your action
}
Run Code Online (Sandbox Code Playgroud)