开发者

how distinguish whether a reference refers to an array or to a single value?

开发者 https://www.devze.com 2023-03-05 23:51 出处:网络
I\'m using WMI and it gives me a ManagementCollectionObject that include a collection of objects in different types such as string, int64, array of int, array of string and so on.

I'm using WMI and it gives me a ManagementCollectionObject that include a collection of objects in different types such as string, int64, array of int, array of string and so on.

WMI Code Generator generates different code for single value and array values. as below:

// getting single value
Console.WriteLine("Availability: {0}", queryObj["Availability"]);


// getting single array value
if(queryObj["AvailableJobSheets"] == null)
   Console.WriteLine("AvailableJobSheets: {0}", queryObj["AvailableJobSheets"]);
else
{
    String[] arrAvailableJobSheets = (String[])(queryObj["AvailableJobSheets"]);
    foreach (String arrValue in arrAvailableJobSheets)
    {
        Console.WriteLine("AvailableJobShe开发者_Python百科ets: {0}", arrValue);
    }
}

how can i distinguish whether the value of a property is array or not?


You could use Type.IsArray:

if(queryObj["AvailableJobSheets"].GetType().IsArray)
    // It's an array
0

精彩评论

暂无评论...
验证码 换一张
取 消