An Object Which Contains Variant Data – Handling Arrays


I struck this one working with WMI (Windows Management Instrumentation) and the C# classes in the System.Management Namespace.

The property value is just and object which can contain a singular value of an array of a values.

This post shows how I addressed the problem, and an interesting feature of C#.

The Object I was working with


What an Array Looks Like


The Code to Dump the Values in the Array

        private static void DumpArrayValues(PropertyData PropArray, int Indent)
            Array x1 = (Array)PropArray.Value;
            String TabIndent = new string('\t', Indent);
            for (int i = 0; i < x1.Length; i++)
                Debug.WriteLine(String.Format("{0} [{1}] {2}", TabIndent, i, x1.GetValue(i)));

Interesting Features


  • Value inherits (I have shown it as a multiple inheritance, I know C# does not support multiple inheritance but that is what it looks like to me) from both System.Array and System.String.
  • Casting the property array object as an Array object, allows you to get the length (number of elements in the array) of the array.
  • String.Format takes care of the different object types.

, , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: