Archive for June, 2011

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.

, , , ,

Leave a comment

%d bloggers like this: