views:

53

answers:

1

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
+1  A: 

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

Thomas Levesque
I apologize, I made a mistake. When I change the CodeTypeDeclaration.Name property, it does change the constructor names. What doesn't get changed are the class static methods used in constructors. For example, if I change the name from 'Service' to 'Win32_Service' one of the constructors looks like: public Win32_Service(string keyName) { this.InitializeObject(null, new System.Management.ManagementPath(Service.ConstructPath(keyName)) , null); } where Service.ConstructPath should be Win32_Service.ConstructPath.
Uros Calakovic
OK, that's a bit harder to solve then... You probably need to visit all nodes in the CodeDom object to replace the type name. I can't think of an easier way to do it...
Thomas Levesque
Thanks, I'll look into it. Just hoped there would be an easier way.
Uros Calakovic