Reputation: 354476
I've implemented a small PowerShell module which brings a custom type with it. I defined the type in the .psm1
file as a C# class and added it with Add-Type
. Now, when I add the module and remove it again, the type is still there which probably isn't quite right (it prevents re-adding the module, for example). The documentation for Remove-Module
states that types defined in assemblies loaded by the module are unloaded as well. But my module doesn't bring in an assembly, just a tiny-ish single type in source code form.
I could just put the type into its own DLL and mark it as an assembly to load in the module manifest, but I like how currently all the source code is readily visible. Distributing a DLL with the module might just raise suspicion why it needs an executable file.
Is there something I can hook onto to remove the type somehow when unloading the module? Or should I just ignore potential errors with Add-Type
to at least being able to re-add the module once removed from a session? I'd rather avoid putting a DLL in there (probably overkill anyway for that tiny module).
Upvotes: 4
Views: 1037
Reputation: 201652
The docs on Remove-Module
also say that the assembly is not unloaded. This is a fundamental issue with .NET and the CLR. Once an assembly is loaded into an AppDomain it can't be unloaded. So creating your own DLL (managed assembly) isn't going to help.
I'm not sure there is much you can do here short of avoiding Add-Type and creating your custom type using new-object psobject -prop @{...}
and $obj.psobject.typenames.insert(0, 'newtypename')
.
Upvotes: 6