ManagementClass.GetStronglyTypedClassCode 方法

定义

为给定的 WMI 类生成强类型类。Generates a strongly-typed class for a given WMI class.

重载

GetStronglyTypedClassCode(Boolean, Boolean)

为给定的 WMI 类生成强类型类。Generates a strongly-typed class for a given WMI class.

GetStronglyTypedClassCode(CodeLanguage, String, String)

为给定的 WMI 类生成强类型类。Generates a strongly-typed class for a given WMI class. 此函数生成 Visual Basic、C#、JScript、J# 或 C++ 的代码,具体情况取决于输入参数。This function generates code for Visual Basic, C#, JScript, J#, or C++ depending on the input parameters.

注解

.NET Framework 安全性.NET Framework Security

对直接调用方的完全信任。Full trust for the immediate caller. 此成员不能由部分信任的代码使用。This member cannot be used by partially trusted code. 有关详细信息,请参阅 从部分受信任的代码使用库For more information, see Using Libraries from Partially Trusted Code.

GetStronglyTypedClassCode(Boolean, Boolean)

为给定的 WMI 类生成强类型类。Generates a strongly-typed class for a given WMI class.

public:
 System::CodeDom::CodeTypeDeclaration ^ GetStronglyTypedClassCode(bool includeSystemClassInClassDef, bool systemPropertyClass);
public System.CodeDom.CodeTypeDeclaration GetStronglyTypedClassCode (bool includeSystemClassInClassDef, bool systemPropertyClass);
member this.GetStronglyTypedClassCode : bool * bool -> System.CodeDom.CodeTypeDeclaration
Public Function GetStronglyTypedClassCode (includeSystemClassInClassDef As Boolean, systemPropertyClass As Boolean) As CodeTypeDeclaration

参数

includeSystemClassInClassDef
Boolean

如果包括用于管理系统属性的类,为 true;否则为 falsetrue to include the class for managing system properties; otherwise, false.

systemPropertyClass
Boolean

如果生成的类将管理系统属性,为 true;否则为 falsetrue to have the generated class manage system properties; otherwise, false.

返回

CodeTypeDeclaration

CodeTypeDeclaration,它表示强类型类的声明。A CodeTypeDeclaration representing the declaration for the strongly-typed class.

示例

下面的示例为 Win32_LogicalDisk 类生成强类型类。The following example generates a strongly-typed class for the Win32_LogicalDisk class. 生成的代码在 c # 或 Visual Basic .NET 中生成。The generated code is produced in C# or Visual Basic .NET.

using System;
using System.Management;
using System.CodeDom;
using System.IO;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace ManagementSample
{
    class GenerateCSharpCode
    {
        static void Main(string[] args)
        {

            string strFilePath = "C:\\temp\\LogicalDisk.cs";
            CodeTypeDeclaration ClsDom;

            ManagementClass cls1 =
                new ManagementClass(null,"Win32_LogicalDisk",null);
            ClsDom = cls1.GetStronglyTypedClassCode(false,false);

            ICodeGenerator cg =
                (new CSharpCodeProvider()).CreateGenerator ();
            CodeNamespace cn = new CodeNamespace("TestNamespace");

            // Add any imports to the code
            cn.Imports.Add(
                new CodeNamespaceImport("System"));
            cn.Imports.Add(
                new CodeNamespaceImport("System.ComponentModel"));
            cn.Imports.Add(
                new CodeNamespaceImport("System.Management"));
            cn.Imports.Add(
                new CodeNamespaceImport("System.Collections"));

            // Add class to the namespace
            cn.Types.Add (ClsDom);

            // Now create the filestream (output file)
            TextWriter tw = new StreamWriter(new
                FileStream (strFilePath,FileMode.Create));

            // And write it to the file
            cg.GenerateCodeFromNamespace(
                cn, tw, new CodeGeneratorOptions());

            tw.Close();
        }
    }
}
Imports System.Management
Imports System.CodeDom
Imports System.IO
Imports System.CodeDom.Compiler
Imports System.Security.Permissions

Namespace Sample

    <EnvironmentPermissionAttribute(SecurityAction.LinkDemand)> _
    Public Class GenerateVBCode

        <EnvironmentPermissionAttribute(SecurityAction.LinkDemand)> _
                Public Overloads Shared Function _
                    Main(ByVal args() As String) As Integer

            Dim strFilePath As String
            strFilePath = "C:\temp\LogicalDisk.vb"
            Dim ClsDom As CodeTypeDeclaration

            Dim cls1 As ManagementClass
            cls1 = New ManagementClass( _
                Nothing, "Win32_LogicalDisk", Nothing)
            ClsDom = cls1.GetStronglyTypedClassCode(False, False)


            Dim cg As ICodeGenerator
            cg = (New VBCodeProvider).CreateGenerator()
            Dim cn As CodeNamespace
            cn = New CodeNamespace("TestNamespace")

            ' Add any imports to the code
            cn.Imports.Add( _
                New CodeNamespaceImport("System"))
            cn.Imports.Add( _
                New CodeNamespaceImport("System.ComponentModel"))
            cn.Imports.Add( _
                New CodeNamespaceImport("System.Management"))
            cn.Imports.Add( _
                New CodeNamespaceImport("System.Collections"))

            ' Add class to the namespace
            cn.Types.Add(ClsDom)

            ' Now create the filestream (output file)
            Dim tw As TextWriter
            tw = New StreamWriter(New _
                FileStream(strFilePath, FileMode.Create))

            Dim options As New CodeGeneratorOptions
            ' And write it to the file
            cg.GenerateCodeFromNamespace( _
            cn, tw, options)

            tw.Close()

        End Function
    End Class
End Namespace

注解

.NET Framework 安全性.NET Framework Security

对直接调用方的完全信任。Full trust for the immediate caller. 此成员不能由部分信任的代码使用。This member cannot be used by partially trusted code. 有关详细信息,请参阅 从部分受信任的代码使用库For more information, see Using Libraries from Partially Trusted Code.

适用于

GetStronglyTypedClassCode(CodeLanguage, String, String)

为给定的 WMI 类生成强类型类。Generates a strongly-typed class for a given WMI class. 此函数生成 Visual Basic、C#、JScript、J# 或 C++ 的代码,具体情况取决于输入参数。This function generates code for Visual Basic, C#, JScript, J#, or C++ depending on the input parameters.

public:
 bool GetStronglyTypedClassCode(System::Management::CodeLanguage lang, System::String ^ filePath, System::String ^ classNamespace);
public bool GetStronglyTypedClassCode (System.Management.CodeLanguage lang, string filePath, string classNamespace);
member this.GetStronglyTypedClassCode : System.Management.CodeLanguage * string * string -> bool
Public Function GetStronglyTypedClassCode (lang As CodeLanguage, filePath As String, classNamespace As String) As Boolean

参数

lang
CodeLanguage

要生成的代码的语言。The language of the code to be generated. 此代码语言来自 CodeLanguage 枚举。This code language comes from the CodeLanguage enumeration.

filePath
String

要在其中编写代码的文件的路径。The path of the file where the code is to be written.

classNamespace
String

将在其中生成该类的 .NET 命名空间。The.NET namespace into which the class should be generated. 如果它为空,则将从 WMI 命名空间生成该命名空间。If this is empty, the namespace will be generated from the WMI namespace.

返回

Boolean

如果方法成功,则为 true;否则为 falsetrue, if the method succeeded; otherwise, false.

示例

下面的示例为 Win32_LogicalDisk 类生成强类型类。The following example generates a strongly-typed class for the Win32_LogicalDisk class. 生成的代码是 c # 中的 c # 示例,并为 Visual Basic .NET 示例 Visual Basic .NET。The generated code is in C# for the C# example and Visual Basic .NET for the Visual Basic .NET example.

using System;
using System.Management;
using System.CodeDom;
using System.IO;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace ManagementSample
{
    class GenerateCSharpCode
    {
        static void Main(string[] args)
        {
            ManagementClass cls1 = new ManagementClass(
                null, "Win32_LogicalDisk",null);
            cls1.GetStronglyTypedClassCode(
                CodeLanguage.CSharp,
                "C:\\temp\\Logicaldisk.cs",
                String.Empty);
        }
    }
}
Imports System.Management
Imports System.CodeDom
Imports System.IO
Imports System.CodeDom.Compiler

Class GenerateVBCode

    Public Overloads Shared Function _
        Main(ByVal args() As String) As Integer

        Dim cls1 As ManagementClass
        cls1 = New ManagementClass( _
            Nothing, "Win32_LogicalDisk", Nothing)
        cls1.GetStronglyTypedClassCode( _
            CodeLanguage.VB, _
            "C:\temp\Logicaldisk.vb", _
            String.Empty)

    End Function
End Class

注解

.NET Framework 安全性.NET Framework Security

对直接调用方的完全信任。Full trust for the immediate caller. 此成员不能由部分信任的代码使用。This member cannot be used by partially trusted code. 有关详细信息,请参阅 从部分受信任的代码使用库For more information, see Using Libraries from Partially Trusted Code.

适用于