共用方式為


繼承

下列程式碼範例顯示使用 Managed 程式碼建立 WMI 繼承階層架構的某些輕微變異。

衍生自 BaseEvent 的類別繼承

如果開發人員想要使用 WMI 公開應用程式事件,該事件必須衍生自 WMI 系統類別 __ExtrinsicEvent。依預設,當您從 System.Management.Instrumentation.BaseEvent 衍生類別時,您的 Managed 類別將代表衍生自 __ExtrinsicEvent 的 WMI 類別。如果您進一步從您自己的類別衍生類別,這些類別將對映到 WMI 事件的繼承樹狀結構。

您 Managed 事件類別階層架構的任何事件都可以從 Managed 程式碼引發。這樣用戶端應用程式便可訂閱各種類似的事件,或訂閱特殊單一種類事件。例如,如果您有兩個事件類別,MyBaseEvent 和衍生類別 MyDerivedEvent,WQL 查詢 "SELECT * FROM MyBaseEvent" 可讓用戶端應用程式抓取從您的應用程式引發的 MyBaseEventMyDerivedEvent 事件。另外,當您的應用程式引發 MyBaseEvent,而且在應用程式引發 MyDerivedEvent 取得事件時,發出 WQL 查詢 "SELECT * FROM MyDerivedEvent" 的用戶端將不會看到任何作業。

下列程式碼範例顯示如何使用繼承來建立使用 BaseEvent 輔助器類別的簡易 WMI 事件階層架構最上層 Managed 事件類別 TopEvent 由衍生自 __ExtrinsicEvent 的 WMI 類別 TopEvent 來表示。TopEvent 類別將擁有兩個子系,Branch1Event 和 Branch2Event。最後,Branch1Event 將有兩個子系 (Leaf1AEvent 和 Leaf1BEvent),而Branch2Event 將有一個子系 (Leaf2AEvent)。若要視覺化呈現這個事件階層架構,您可以在範例的第一個註解區塊中檢視圖表。階層架構 (不具有進一步衍生類別的類別) 中大多數的衍生類別參考是指分葉節點。事件階層架構的分葉節點並不特別重要,但在進行執行個體副稽核時,分葉節點和非分葉節點之間的區別則扮演著重要的角色。

為簡化作業,這些事件不具有任何屬性。實際作業時,您可能會擴充所有子系類別的其他屬性,但在下列程式碼中,為簡化該範例,我們將省略這項步驟。

using System;
using System.Management.Instrumentation;

[assembly:Instrumented]

// A simple tree of events derived from BaseEvent
// TopEvent--Branch1Event--Leaf1AEvent
//          \            \-Leaf1BEvent
//           \
//            Branch2Event--Leaf2AEvent
// Any event in the hierarchy can be raised by the application

// This class inherits the 'InstrumentationType.Event' attribute
// from 'BaseEvent'
public class TopEvent : BaseEvent {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Branch1Event : TopEvent{
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Leaf1AEvent : Branch1Event {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Leaf1BEvent : Branch1Event {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Branch2Event : TopEvent {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Leaf2AEvent : Branch2Event {
}

class App {
    static void Main(string[] args) {
        // Raise each type of event
        new TopEvent().Fire();
        new Branch1Event().Fire();
        new Leaf1AEvent().Fire();
        new Leaf1BEvent().Fire();
        new Branch2Event().Fire();
        new Leaf2AEvent().Fire();
    }
}

非衍生自 BaseEvent 的類別事件繼承

除了從輔助器類別 System.Managment.Instrumentation.BaseEvent 衍生最上層的事件類別,開發人員可以標記屬性 [InstrumentationClass(InstrumentationType.Event)] 給類別。這項作業只須在最上層的事件類別上進行。自具有這個屬性的類別所衍生而來的類別也會被解譯為副稽核事件類別。請注意,這是因為 InstrumentationClass 屬性會自動繼承衍生的類別。事實上,由於 BaseEvent 本身擁有 InstrumentationClass 屬性 (並會繼承至其所有子系),因此該範例 (衍生自 BaseEvent 的事件) 根本不會用到這個屬性。

下列範例將完成與上一個範例相同的工作,但它將使用 InstrumentationClass 屬性,而非從 BaseEvent 衍生而來:

using System;
using System.Management.Instrumentation;

[assembly:Instrumented]

// A simple tree of events declared with attributes
// TopEvent2--Branch1Event2--Leaf1AEvent2
//           \             \-Leaf1BEvent2
//            \
//             Branch2Event2--Leaf2AEvent2
// Any event in the hierarchy can be raised by the application

// This is a top-level event class
[InstrumentationClass(InstrumentationType.Event)]
public class TopEvent2 {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Branch1Event2 : TopEvent2{
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Leaf1AEvent2 : Branch1Event2 {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Leaf1BEvent2 : Branch1Event2 {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Branch2Event2 : TopEvent2 {
}

// This class inherits the 'InstrumentationType.Event' attribute
public class Leaf2AEvent2 : Branch2Event2 {
}

class App {
    static void Main(string[] args) {
        // Raise each type of event
        Instrumentation.Fire(new TopEvent2());
        Instrumentation.Fire(new Branch1Event2());
        Instrumentation.Fire(new Leaf1AEvent2());
        Instrumentation.Fire(new Leaf1BEvent2());
        Instrumentation.Fire(new Branch2Event2());
        Instrumentation.Fire(new Leaf2AEvent2());
    }
}

衍生自執行個體的類別執行個體繼承

建立執行個體階層架構和事件階層架構有很多相似處。最大的不同為最上層 WMI 事件是衍生自 WMI 類別 __ExtrinsicEvent,而最上層執行個體沒有父代類別 (或使用 WMI 術語稱為超類別)。藉由從輔助器類別 System.Management.Instrumentation.Instance 衍生單一類別來進行簡易執行個體副稽核時,WMI 類別將成為單一最上層執行個體類別。

但執行個體階層架構還要在更複雜一些。在 System.Management 的目前版本中,只有執行個體階層架構的分葉節點可以實際支援執行個體發行。非分葉節點為抽象 WMI 類別。換句話說,如果您有兩個類別 BaseInstance 和衍生類別 DerivedInstance,您可以建立類別 DerivedInstance (但非 BaseInstance) 的執行個體。另一個用來說明支援執行個體的通用詞彙為具體類別。執行個體副稽核的繼承規則說明如下:只有繼承階層架構的分葉節點可以是具體執行個體類別。階層結構的所有其他節點都必須標記為抽象 WMI 類別。

若要標記類別為抽象 WMI 副稽核類別,該類別必須擁有 [InstrumentationClass(InstrumentationType.Abstract)] 屬性。輔助器類別 Instance 被標記為 [InstrumentationClass(InstrumentationType.Instance)],並依預設將它填入子系類別。這種情況發生在您使用單一最上層具體執行個體類別時並沒有問題,但如果是在較複雜的階層架構中,您必須使用手動的方式新增 Abstract 屬性到最上層類別。下列範例展示使用這項技術的階層結構。最上層類別 TopInstance 衍生自 Instance,並且明確標記為 Abstract。非分葉節點的衍生類別將正確繼承這個屬性 (並且將成為 WMI 類別,與 TopInstance 相似)。分葉節點必須標記為具體執行個體,因此其類別定義將明確使用 Instance 屬性。

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;
    }
}

非衍生自執行個體的類別執行個體繼承

如果您僅使用屬性來宣告執行個體副稽核類別,階層架構的定義將與衍生自輔助器類別 System.Management.Instrumentation.Instance 的執行個體副稽核的方法相似。同樣的,除非您只有一個最上層的具體執行個體類別,否則最上層的執行個體副稽核類別應標記為 [InstrumentationClass(InstrumentationType.Abstract)]。階層架構的分支將繼承這個屬性,因此它們並不需要明確宣告其 InstrumentationClass 屬性。分葉節點將必須再次標記為具體的 WMI 執行個體,而且必須使用 [InstrumentationClass(InstrumentationType.Instance)] 屬性。

下列範例顯示和上述範例相同的功能,但下列範例僅使用 InstrumentationClass 屬性,而非從輔助器類別 Instance 衍生最上層類別。

using System;
using System.Management.Instrumentation;

[assembly:Instrumented]

// A simple tree of instances declared with attributes
// TopInstance2--Branch1Instance2--Leaf1AInstance2
//           \                   \-Leaf1BInstance2
//            \
//             Branch2Instance2--Leaf2AInstance2
// 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.
[InstrumentationClass(InstrumentationType.Abstract)]
public class TopInstance2 {
}

// This class inherits the 'InstrumentationType.Abstract' attribute
public class Branch1Instance2 : TopInstance2{
}

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

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

// This class inherits the 'InstrumentationType.Abstract' attribute
public class Branch2Instance2 : TopInstance2 {
}

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

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

        Instrumentation.Publish(leaf1a);
        Instrumentation.Publish(leaf1b);
        Instrumentation.Publish(leaf2a);

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

請參閱

使用 System.Management 副稽核 .NET Framework 應用程式 | CLI 和 WMI 中的類別和對應 | 公開管理事件 | 公開管理資料 | 註冊副稽核應用程式的結構描述