公開管理資料
在資料管理案例中,開發人員可能想要公開管理類別。
.NET Framework 類別將定義、實作和標記為管理。您可以使用類別的 InstrumentationClass(InstrumentationType.Instance) 屬性,或者從 Instance 類別衍生該類別來完成上述作業。它將自動產生 Managed 程式碼類別的結構描述。該類別的執行個體將公開為 WMI 執行個體,並且對應所有屬性值。
副稽核類別的定義將於設計階段完成 (編譯時間),它是應用程式負責事項的前半段。在 Runime,應用程式必須提供實際資料。Managed 程式碼開發人員並不需學習新的方法在 WMI 可消耗的結構中格式化資料。Managed 應用程式使用與定義 WMI 副稽核類別相同的 Managed 類別,藉由執行個體化 (Instantiate) 類別和填入欄位來提供資料。透過 WMI 公開的資料可直接從 Managed 類別欄位和屬性存取。
下表列出 Managed 程式碼開發人員使用 WMI 將副稽核放在表層時所需完整的基本需求:
定義說明副稽核的 Managed 類別 (使用任何支援的語言)。
不需了解 WMI 方法或定義類別的 WMI 文法便可將該類別對應到 WMI 類別。
在 Runtime,資訊將藉由執行個體化類別、填入 (Populate) 欄位和發行執行個體放到 WMI 表層。
下列程式碼範例展示如何建立管理副稽核執行個體類別和如何發行該類別的執行個體到 WMI:
using System;
using System.Management;
using System.Configuration.Install;
using System.Management.Instrumentation;
// This example demonstrate how to define a management instrumentation
// class and how to publish an instance of this class to WMI.
// Specify which namespace the data should be published into
[assembly:Instrumented("root/default")]
// Let the system know InstallUtil.exe utility will be run against
// this assembly
[System.ComponentModel.RunInstaller(true)]
public class MyInstaller : DefaultManagementProjectInstaller {}
// Define a management instrumentation class
[InstrumentationClass(InstrumentationType.Instance)]
public class InstanceClass {
public string Name;
public int Number;
}
public class Sample_InstanceProvider {
public static int Main(string[] args) {
InstanceClass instClass = new InstanceClass();
instClass. Name = "Hello";
instClass. Number = 888;
// Publish this instance to WMI
Instrumentation.Publish(instClass);
Console.WriteLine("Instance now visible through WMI");
Console.ReadLine();
Instrumentation.Revoke(instClass); //optional
return 0;
}
}
[Visual Basic]
Imports System
Imports System.Management
Imports System.Configuration.Install
Imports System.Management.Instrumentation
' This example demonstrate how to define a management instrumentation
' class and how to publish an instance of this class to WMI.
' Specify which namespace the data should be published into
<assembly: Instrumented("Root/Default")>
' Let the system know InstallUtil.exe utility will be run against
' this assembly
<System.ComponentModel.RunInstaller(True)> _
Public Class MyInstaller
Inherits DefaultManagementProjectInstaller
End Class
' Create a management instrumentation instance class
<InstrumentationClass(InstrumentationType.Instance)> _
Public Class InstanceClass
Public Name As String
Public Number As Integer
End Class
Public Class Sample_InstanceProvider
Overloads Public Shared Function Main(args() As String) As Integer
Dim instClass As New InstanceClass()
instClass.Name = "Hello"
instClass.Number = 888
' Publish this instance to WMI
System.Management.Instrumentation.Instrumentation.Publish(instClass)
Console.WriteLine("Instance now visible through WMI")
Console.ReadLine()
System.Management.Instrumentation.Instrumentation.Revoke(instClass)
Return 0
End Function
End Class
請參閱
使用 System.Management 副稽核 .NET Framework 應用程式 | CLI 和 WMI 中的類別和對應 | 公開管理事件 | 繼承 | 註冊副稽核應用程式的結構描述