Change Generated C# Class Name
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 he开发者_运维百科re 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
I think you can rename the constructors manually by enumerating the Members
property and changing the Name
property for members of type CodeConstructor
精彩评论