更改生成的 C# 类名称

发布于 2024-09-26 02:01:02 字数 672 浏览 2 评论 0原文

我正在使用 ManagementClass.GetStronglyTypedClassCode 来获取对 CodeTypeDeclaration 实例的引用。我想更改生成的类名,但此方法不允许这样做(据我所知)。我尝试更改 CodeTypeDeclaration.Name 属性,但这不会更改构造函数名称等,因此 C# 编译器在编译类时返回错误。我可以对类进行简单的搜索/替换,但这似乎不可靠。有没有办法可靠地更改由 CodeTypeDeclaration 实例表示的 C# 类的名称?

代码不多,但这是相关的代码片段(我使用的是 PowerShell):

$WmiClassName = 'Win32_Process'
$ClassName = 'MyProcess'

$WmiClass = New-Object `
    System.Management.ManagementClass($WmiClassName)

$classCode = $WmiClass.GetStronglyTypedClassCode($true, $false)
$classCode.Name = $ClassName # This doesn't change constructor names

I am using ManagementClass.GetStronglyTypedClassCode to get a reference to CodeTypeDeclaration instance. I would like to change the generated class name and this method doesn't allow for this (as far as I can tell). I have tried to change the CodeTypeDeclaration.Name property, but this doesn't change constructor names etc, so the C# compiler returns errors when the class is compiled. I could do a simple search / replace on the class, but this seems unreliable. Is there a way to reliably change the name of a C# class represented by a CodeTypeDeclaration instance?

There isn't much code, but here is the relevant snippet (I am using PowerShell):

$WmiClassName = 'Win32_Process'
$ClassName = 'MyProcess'

$WmiClass = New-Object `
    System.Management.ManagementClass($WmiClassName)

$classCode = $WmiClass.GetStronglyTypedClassCode($true, $false)
$classCode.Name = $ClassName # This doesn't change constructor names

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-10-03 02:01:02

我认为您可以通过枚举 Members 属性并更改 CodeConstructor 类型成员的 Name 属性来手动重命名构造函数

I think you can rename the constructors manually by enumerating the Members property and changing the Name property for members of type CodeConstructor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文