Inheritance for Classes Derived from Instance

The creation of instance hierarchies has many similarities to the creation of event hierarchies. The main difference is that top-level WMI events are derived from the WMI class __ExtrinsicEvent, and top-level instances have no parent class (or superclass, in WMI terminology). For more information, see "__ExtrinsicEvent" in the Windows Management Instrumentation documentation in the MSDN Library at https://msdn.microsoft.com/library. When doing simple instance instrumentation by deriving a single class from the Instance helper class in the System.Management.Instrumentation namespace, the WMI class appears as a single top-level instance class.

Instance hierarchies, however, are slightly more complex. In the current version of System.Management, only leaf nodes of an instance hierarchy can actually support the publication of instances. The non-leaf nodes are considered abstract WMI classes. In other words, if you have two classes, BaseInstance and a derived class DerivedInstance, you can create instances of the class DerivedInstance but not BaseInstance. Another term commonly used to describe classes that can support instances is concrete class. The inheritance rule for instance instrumentation can then be phrased as follows: Only leaf nodes of an inheritance hierarchy can be concrete instance classes. All other nodes of the hierarchy must be marked as abstract WMI classes.

To mark a class as an abstract WMI instrumentation class, it must have the [InstrumentationClassAttribute(InstrumentationType.Abstract)] attribute. The helper class Instance is marked with [InstrumentationClassAttribute(InstrumentationType.Instance)], which propagates to child classes by default. This is fine when you have a single top-level concrete instance class, but with a more complex hierarchy, you have to manually add the Abstract attribute to the top-level class.

The following code example demonstrates a hierarchy that manually adds the Abstract attribute to the top-level class. The top-level class, TopInstance, is derived from the Instance class and explicitly marked with Abstract. The derived classes that are not leaf nodes will correctly inherit this attribute (and will be abstract WMI classes like TopInstance). The leaf nodes must be marked as concrete instances, and therefore will have explicit Instance attributes on their class definitions.

Imports System.Management.Instrumentation
Imports System

<Assembly: Instrumented()> 

' A simple tree of instances derived from Instance
' TopInstance--Branch1Instance--Leaf1AInstance
'           \                 \-Leaf1BInstance
'            \
'             Branch2Instance--Leaf2AInstance
' Only the leafs of the tree can be concrete instances.
' All other nodes on the tree must be abstract.

' This is a top-level abstract class.  It must have the
' 'InstrumentationType.Abstract' attribute or it would
' inherit the 'Instance' 
' attribute from the base class 'Instance'
<InstrumentationClass(InstrumentationType.Abstract)> _
Public Class TopInstance
    Inherits Instance

End Class


' This class inherits the 
' 'InstrumentationType.Abstract' attribute
Public Class Branch1Instance
    Inherits TopInstance

End Class


' This is a leaf class which can be concrete
<InstrumentationClass(InstrumentationType.Instance)> _
Public Class Leaf1AInstance
    Inherits Branch1Instance

End Class

' This is a leaf class which can be concrete
<InstrumentationClass(InstrumentationType.Instance)> _
Public Class Leaf1BInstance
    Inherits Branch1Instance

End Class

' This class inherits the 
' 'InstrumentationType.Abstract' attribute
Public Class Branch2Instance
    Inherits TopInstance
End Class

' This is a leaf class which can be concrete
<InstrumentationClass(InstrumentationType.Instance)> _
Public Class Leaf2AInstance
    Inherits Branch2Instance

End Class

Class App
    Public Overloads Shared Function _
        Main(ByVal args() As String) As Integer
        ' Publish an instance of each leaf
        Dim leaf1a As New Leaf1AInstance
        Dim leaf1b As New Leaf1BInstance
        Dim leaf2a As New Leaf2AInstance

        leaf1a.Published = True
        leaf1b.Published = True
        leaf2a.Published = True

        ' Instances now visible through WMI
        Console.WriteLine("Instances now visible through WMI")
        Console.ReadLine()

        ' Revoke all instances
        leaf1a.Published = False
        leaf1b.Published = False
        leaf2a.Published = False

    End Function
End Class
using System;
using System.Management.Instrumentation;

[assembly:Instrumented]

// A simple tree of instances derived from Instance
// TopInstance--Branch1Instance--Leaf1AInstance
//           \                 \-Leaf1BInstance
//            \
//             Branch2Instance--Leaf2AInstance
// Only the leafs of the tree can be concrete instances.
// All other nodes on the tree must be abstract.

// This is a top-level abstract class.  It must have the
// 'InstrumentationType.Abstract' attribute or it would
// inherit the 'Instance' attribute 
// from the base class 'Instance'
[InstrumentationClass(InstrumentationType.Abstract)]
public class TopInstance : Instance
{
}

// This class inherits the 
// 'InstrumentationType.Abstract' attribute
public class Branch1Instance : TopInstance 
{
}

// This is a leaf class which can be concrete
[InstrumentationClass(InstrumentationType.Instance)]
public class Leaf1AInstance : Branch1Instance 
{
}

// This is a leaf class which can be concrete
[InstrumentationClass(InstrumentationType.Instance)]
public class Leaf1BInstance : Branch1Instance 
{
}

// This class inherits the 
// 'InstrumentationType.Abstract' attribute
public class Branch2Instance : TopInstance 
{
}

// This is a leaf class which can be concrete
[InstrumentationClass(InstrumentationType.Instance)]
public class Leaf2AInstance : Branch2Instance 
{
}

class App {
    static void Main(string[] args) 
    {
        // Publish an instance of each leaf
        Leaf1AInstance leaf1a = new Leaf1AInstance();
        Leaf1BInstance leaf1b = new Leaf1BInstance();
        Leaf2AInstance leaf2a = new Leaf2AInstance();

        leaf1a.Published = true;
        leaf1b.Published = true;
        leaf2a.Published = true;

        // Instances now visible through WMI
        Console.WriteLine("Instances now visible through WMI");
        Console.ReadLine();
        
        // Revoke all instances
        leaf1a.Published = false;
        leaf1b.Published = false;
        leaf2a.Published = false;
    }
}

See Also

Tasks

How to: Provide Management Data

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.