Question:
I hava a cs file with very simple code:
1 2 3 4 5 6 7 8 9 |
using Ionic.Zip; public static class Helper { public static ZipFile GetNewFile(string fileName) { return new ZipFile(fileName); } } |
It requires Ionic.Zip assembly. I want to add this type to my powershell like this:
1 2 3 4 |
cd c:\pst Add-Type -Path "2.cs" -ReferencedAssemblies "Ionic.Zip.dll" $var = [Helper]::GetNewFile("aaa") |
When I do this it gives me:
1 2 3 |
The following exception occurred while retrieving member "GetNewFile": "Could not load file or assembly 'Ionic.Zip, Version=1.9.1.8, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c' or one of its dependencies. The located assembly' s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" |
It seems to have compiled assembly in some temp location and it can’t find Ionic.Zip there.
It works, however, if specify the output assembly and then add this assembly:
1 2 3 4 5 6 7 |
cd c:\pst Add-Type -Path "2.cs" -ReferencedAssemblies "Ionic.Zip.dll" -OutputAssembly "T.dll" Add-Type -Path "T.dll" $var = [Helper]::GetNewFile("aaa") $var.AlternateEncoding |
So I’m wondering if there’s a way to avoid usage of output assembly?
Answer:
In Powershell v3 CTP1 you can resolve the full path (fullname) of your zip library and reference that:
1 2 3 4 5 6 |
$ziplib = (get-item ionic.zip.dll).fullname [void][reflection.assembly]::LoadFrom($ziplib) Add-Type -Path "2.cs" -ReferencedAssemblies $ziplib $var = [Helper]::GetNewFile("aaa") $var.AlternateEncoding |