G
Guest
Hi,
Here are sections of my codes:
private void test()
{
ObjectQuery objectQuery = new ObjectQuery("select * from
Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
foreach (ManagementObject share in searcher.Get())
{
AppendObject(share);
}
}
private void AppendObject(ManagementObject obj)
{
PropertyDataCollection dataCollection = obj.Properties;
foreach (PropertyData data in dataCollection)
{
if (data.IsArray)
{
object[] tempArray = (object[])(obj[data.Name]);
if (tempArray != null)
{
AppendString(textBox1, data.Name);
foreach (object temp in tempArray)
{
if (temp != null)
Debug.WriteLine(temp.ToString());
}
}
}
else
{
object temp = obj[data.Name];
if (temp != null)
{
AppendString(textBox1, data.Name + " = " + temp.ToString());
}
else
AppendString(textBox1, data.Name);
}
}
AppendString(textBox1, "");
}
My question is why do I got an "System.InvalidCastException" when the code
reaches the line:
object[] tempArray = (object[])(obj[data.Name]);
when data.Name is GatewayCostMetric.
According to platform SDK, GatewayCostMetric is an array of uint16 and the
in fact the Type property for the data.Type is CimType.UInt16.
I have trace through and be able to cast array of strings to array of
objects. Why is this cast invalid?
Any one can help me on this? Thanks.
Here are sections of my codes:
private void test()
{
ObjectQuery objectQuery = new ObjectQuery("select * from
Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
foreach (ManagementObject share in searcher.Get())
{
AppendObject(share);
}
}
private void AppendObject(ManagementObject obj)
{
PropertyDataCollection dataCollection = obj.Properties;
foreach (PropertyData data in dataCollection)
{
if (data.IsArray)
{
object[] tempArray = (object[])(obj[data.Name]);
if (tempArray != null)
{
AppendString(textBox1, data.Name);
foreach (object temp in tempArray)
{
if (temp != null)
Debug.WriteLine(temp.ToString());
}
}
}
else
{
object temp = obj[data.Name];
if (temp != null)
{
AppendString(textBox1, data.Name + " = " + temp.ToString());
}
else
AppendString(textBox1, data.Name);
}
}
AppendString(textBox1, "");
}
My question is why do I got an "System.InvalidCastException" when the code
reaches the line:
object[] tempArray = (object[])(obj[data.Name]);
when data.Name is GatewayCostMetric.
According to platform SDK, GatewayCostMetric is an array of uint16 and the
in fact the Type property for the data.Type is CimType.UInt16.
I have trace through and be able to cast array of strings to array of
objects. Why is this cast invalid?
Any one can help me on this? Thanks.