使用CodeDom更改C#类名称 [英] Use CodeDom to Change a C# Class Name

查看:75
本文介绍了使用CodeDom更改C#类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Management.ManagementClass.GetStronglyTypedClassCode [ ^ ]
获取对包含强类型WMI类声明的CodeTypeDeclaration实例的引用.此方法不允许更改生成的C#类名称.例如,如果生成了Win32_Service WMI类的代码,则所得的C#类将始终命名为Service.我希望能够更改结果的类名.我试图将CodeTypeDeclaration.Name属性更改为"Win32_Service":



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:


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



Service.ConstructPath 是静态方法,应将表达式更改为Win32_Service.ConstructPath.

另外,另一种静态方法:



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



仍然具有Service的返回类型,而不是Win32_Service.我可以尝试进行搜索/替换,但是生成的类中的其他成员不需要更改(例如StartService,StopService等),并且由于具有不同的WMI类的数量,我不确定如何处理此问题.有没有可靠的方法来更改由CodeTypeDeclaration实例表示的类的名称?如果唯一的方法是浏览类元素,我将如何访问并将表达式Service.ConstructPath更改为Win32_Service.ConstructPath (在第二个代码段中)?



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

推荐答案

声音听起来像是用"_"字符分隔您的名字.尝试命名名称中不含_的类.也许是"Win32Service".
Sounds and looks like it is splitting you name by "_" char. Try naming the class without the _ in the name. Perhaps "Win32Service".


这篇关于使用CodeDom更改C#类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆