Redigera

Dela via


ManagementClass.GetStronglyTypedClassCode Method

Definition

Generates a strongly-typed class for a given WMI class.

Overloads

GetStronglyTypedClassCode(Boolean, Boolean)

Generates a strongly-typed class for a given WMI class.

GetStronglyTypedClassCode(CodeLanguage, String, String)

Generates a strongly-typed class for a given WMI class. This function generates code for Visual Basic, C#, JScript, J#, or C++ depending on the input parameters.

Remarks

.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)

Source:
ManagementClass.cs
Source:
ManagementClass.cs

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

Parameters

includeSystemClassInClassDef
Boolean

true to include the class for managing system properties; otherwise, false.

systemPropertyClass
Boolean

true to have the generated class manage system properties; otherwise, false.

Returns

A CodeTypeDeclaration representing the declaration for the strongly-typed class.

Examples

The following example generates a strongly-typed class for the Win32_LogicalDisk class. 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

Remarks

.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.

Applies to

GetStronglyTypedClassCode(CodeLanguage, String, String)

Source:
ManagementClass.cs
Source:
ManagementClass.cs

Generates a strongly-typed class for a given WMI class. 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

Parameters

lang
CodeLanguage

The language of the code to be generated. This code language comes from the CodeLanguage enumeration.

filePath
String

The path of the file where the code is to be written.

classNamespace
String

The.NET namespace into which the class should be generated. If this is empty, the namespace will be generated from the WMI namespace.

Returns

true, if the method succeeded; otherwise, false.

Examples

The following example generates a strongly-typed class for the Win32_LogicalDisk class. 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

Remarks

.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.

Applies to