BUG: Windows management instrumentation does not support all .NET data types (307504)



The information in this article applies to:

  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Windows Management Instrumentation in . NET

This article was previously published under Q307504

SYMPTOMS

When you use Windows Management Instrumentation (WMI) in Visual Studio 2005 or in Visual Studio .NET, you may receive a compile error on a valid .NET data type.

CAUSE

Some data types, such as Decimal, do not have an equivalent representation in WMI. If you want to expose properties of unsupported data types through instrumentation, you must supply your own conversion function that maps the unsupported data type to a supported instrumentation data type.

RESOLUTION

To work around this problem, expose the unsupported data type through instrumentation by using properties to cast to a supported data type. You can use this technique to expose members as an alternate data type, such as by exposing an Integer as a Double, or by exposing a DateTime as a String. The following example shows how you can expose a Decimal field on a class as a Double value through WMI.
public class DecimalSample : Instance
	{
	    [IgnoreMember] public Decimal amount;
	    [ManagedName("amount")] public double _amount
	    {
	        get
	        {
	            return (double)amount;
	        }
	    }
	}
	
	class App
	{
	    static void Main()
	    {
	        DecimalSample instance = new DecimalSample();
	        instance.amount = Decimal.Parse("7.310000");
	        instance.Published = true;
	        Console.WriteLine("Amount = {0}", instance.amount);
	        Console.WriteLine("An instance of DecimalSample is now published to WMI.");
	        Console.ReadLine();
	        instance.Published = false;
	    }
	}
				
You cannot expose the field Amount directly through instrumentation because Decimal is not a supported data type. The field Amount must have the IgnoreMember attribute. To expose an approximate value of the Amount field through WMI, define an _amount property that returns the value of Amount cast to a Double.

To make the WMI class appear as similar to the managed class as possible, use the ManagedName attribute to expose the _amount field as a WMI property named Amount. The end result is that you can access the Amount field of the DecimalSample class through managed code as a Decimal field, and WMI clients see a WMI property on the class Amount that contains the value cast to a Double.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

Modification Type:MajorLast Reviewed:3/4/2006
Keywords:kbtshoot kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbbug kbnofix kbreadme KB307504 kbAudDeveloper