Paste in Int32

I am trying to extract data from MSNdis_CurrentPacketFilter, my code is as follows:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI",
                "SELECT NdisCurrentPacketFilter FROM MSNdis_CurrentPacketFilter");

foreach (ManagementObject queryObj in searcher.Get())
{
     uint obj = (uint)queryObj["NdisCurrentPacketFilter"];
     Int32 i32 = (Int32)obj;
}

As you can see, I drop the received object from NdisCurrentPacketFilter twice , which begs the question: why ??

If I try to apply it directly to int, for example:

Int32 i32 = (Int32)queryObj["NdisCurrentPacketFilter"];

He throws out InvalidCastException. Why is this?

+4
source share
1 answer

Three things make this not work for you:

  • Type NdisCurrentPacketFilter- uint, according to this link .

  • queryObj["NdisCurrentPacketFilter"] object, uint, NdisCurrentPacketFilter.

  • , .. - :


, , -

object obj = (uint)12345;
uint unboxedToUint = (uint)obj; // this is fine as we're unboxing to the same type
int unboxedToInt = (int)obj; // this is not fine since the type of the boxed reference type doesn't match the type you're trying to unbox it into
int convertedToInt = Convert.ToInt32(obj); // this is fine
+9

All Articles