Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you remove an Add-ed Type in PowerShell again?

I'm currently writing a library in C# and was using PowerShell to quickly test it on some occasions. However, this prevents me from re-building the project as PowerShell obviously still has the DLL open.

Is there a way of unloading the DLL again after adding it with Add-Type? The documentation doesn't seem to have clues on that and the obvious candidate would be Remove-Type (which doesn't exist – there is only one command anyway with Type as its noun). It gets cumbersome to close PowerShell and do all the stuff of navigating to the build directory and adding the type again each time I want to rebuild.

like image 570
Joey Avatar asked Jul 30 '10 08:07

Joey


People also ask

How do you delete a type in PowerShell?

You can pipe type names to Remove-TypeData . When you pipe an object to Remove-TypeData , Remove-TypeData gets the type name of the object and removes all type data for the object type. Shows what would happen if the cmdlet runs. The cmdlet is not run.

How do I remove type?

1. Press the "Ins" key to toggle overtype mode off. Depending on your keyboard model, this key may also be labeled "Insert." If you simply want to disable overtype mode but keep the ability to toggle it back on, you are done.

What does add-type do in PowerShell?

The Add-Type cmdlet lets you define a Microsoft . NET Core class in your PowerShell session. You can then instantiate objects, by using the New-Object cmdlet, and use the objects just as you would use any .

How do I unload an assembly in PowerShell?

To unload an assembly in the . NET Framework, you must unload all of the application domains that contain it. To unload an application domain, use the AppDomain. Unload method.


2 Answers

Like the others say, this is a .NET behavior. Assemblies loaded into an AppDomain cannot be unloaded. Only the AppDomain can be unloaded, and powershell uses a single appdomain. I blogged a bit about this some years ago:

https://web.archive.org/web/20170707034334/http://www.nivot.org/blog/post/2007/12/07/WhyAppDomainsAreNotAMagicBullet

When I test like this, I usually keep a shell open and use a nested shell to do tests. start powershell, cd to bin location then run "powershell" to start nested shell (new process.) "exit" to start over, and run "powershell" again.

like image 163
x0n Avatar answered Oct 14 '22 07:10

x0n


I find the simplest way to get around this problem is to wrap the Add-Type and the test code inside of a Start-Job. Start-Job will create a background process, and the type will be loaded there. Once you are done, the process goes away and you're free to retry.

Here's an example of how it looks:

$job = Start-Job -ScriptBlock {      Add-Type -path 'my.dll'     $myObj = new-object My.MyTestClassName      $result = $myObj.TestMethod     $result } Wait-Job $job Receive-Job $job 

The output from the test method will be echoed to the console.

like image 44
Start-Automating Avatar answered Oct 14 '22 07:10

Start-Automating