Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using System.Management.ManagementClass.GetStronglyTypedClassCode [^]
to get a reference to a CodeTypeDeclaration instance that holds a strongly typed WMI class declaration. This method doesn't allow for the generated C# class name to be changed. For example, if the code for the Win32_Service WMI class is generated the resulting C# class is always named as Service. I would like to be able to change the resulting class name. I have attempred to change the CodeTypeDeclaration.Name property to 'Win32_Service':



using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Management;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementClass c = 
                new ManagementClass("root\\cimv2:win32_service");
            CodeTypeDeclaration code = 
                c.GetStronglyTypedClassCode(true, false);
            code.Name = "Win32_Service";
            StringWriter sw = new StringWriter();
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            CSharpCodeProvider provider = 
                new CSharpCodeProvider();
            provider.GenerateCodeFromType(code, sw, options);
            Console.WriteLine(sw.ToString());
        }
    }
}


This renames the constructors but leaves the old class name (Service) elsewhere resulting in compiler errors. For instance this is what one of the constructors looks like after renaming the class:


C#
public Win32_Service(string keyName) {
            this.InitializeObject(null, new System.Management.ManagementPath(Service.ConstructPath(keyName))
, null);


Service.ConstructPath is a static method and the expression should be changed to Win32_Service.ConstructPath.

Also, another static method:

[Browsable(true)]
public static Service CreateInstance() {
    System.Management.ManagementScope mgmtScope = null;
    if ((statMgmtScope == null)) {
        mgmtScope = new System.Management.ManagementScope();
        mgmtScope.Path.NamespacePath = CreatedWmiNamespace;
    }
    else {
        mgmtScope = statMgmtScope;
    }
    System.Management.ManagementPath mgmtPath = new System.Management.ManagementPath(CreatedClassName);
    System.Management.ManagementClass tmpMgmtClass = new System.Management.ManagementClass(mgmtScope, mgmtPath, null);
    return new Service(tmpMgmtClass.CreateInstance());
}


still has the return type of Service instead of Win32_Service. I could try to do a search / replace but there are other members of the generated class that need not be changed (like StartService, StopService etc) and with the number of different WMI classes I'm not really sure how to go about this. Is there a reliable way to change the name of a class represented by a CodeTypeDeclaration instance? If the only way is to browse the class elements, how would I access and change the expression Service.ConstructPath to Win32_Service.ConstructPath (in the second code snippet)?
Posted

1 solution

Sounds and looks like it is splitting you name by "_" char. Try naming the class without the _ in the name. Perhaps "Win32Service".
 
Share this answer
 
Comments
Uros Calakovic 2-Oct-10 7:38am    
I did try that, but still the same result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900