Why can't I pull ushort out of System.Object and then use it as uint? (FROM#)

I am manipulating the items in the list which System.Management.ManagementObjectCollection. Each of these elements represents a System.Management.ManagementObjectone that contains properties indexed by a string. Cm:

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"];
    ...
    ...
    ...
}

Each "access to the dictionary" before queryObjreturns C # object, which in fact is what the property should be - I must know their "real" type in advance and this is normal.

The problem is that I get a InvalidCastExceptionin uint. I have to use the real type, which ushort. Should the personality from ushortto be acceptable and obvious uint?

, , string, , uint int long?

+3
2

unbox , ushort.

, , .

T T ( Nullable).

.

+18

, : . , . , :

uint spmajor = (uint)(ushort)queryObj["ServicePackMajorVersion"];

:

uint spmajor = Convert.ToUInt32(queryObj["ServicePackMajorVersion"]);
+2

Source: https://habr.com/ru/post/1711834/


All Articles