更改生成的 C# 类名称
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以通过枚举
Members
属性并更改CodeConstructor
类型成员的Name
属性来手动重命名构造函数I think you can rename the constructors manually by enumerating the
Members
property and changing theName
property for members of typeCodeConstructor