我正在操作列表中的项目System.Management.ManagementObjectCollection
。这些项目中的每一个都是System.Management.ManagementObject
其中包含按字符串索引的属性。看:
foreach (ManagementObject queryObj in searcher.Get())
{
string osversion = (string)queryObj["Version"];
string os = (string)queryObj["Name"];
uint spmajor = (uint)queryObj["ServicePackMajorVersion"];
uint spminor = (uint)queryObj["ServicePackMinorVersion"];
...
...
...
}
每个“字典访问”queryObj
返回一个 C#object
事实上,无论属性应该是什么——我必须事先知道它们的“真实”类型,这没关系。
问题是,我得到一个InvalidCastException
in the uint
演员表。我必须使用真实类型,即ushort
。不应该从ushort
to uint
可以接受并且显而易见吗?
在这种情况下,我最终会将这些值转换为string
,但是如果我必须让他们进入怎么办?uint
or int
or long
变量?
你正在尝试unboxushort,并且只能将其拆箱为 ushort。
拆箱后,您就可以像平常一样投射它。
任何 T 类型的装箱值只能拆箱为 T(或 Nullable)。
埃里克·利珀特(Eric Lippert)就这件事写了一篇非常好的博客文章here http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)