Andrey Marchuk
Andrey Marchuk

Reputation: 13483

Add-Type from cs, that requires referencing assemblies

I hava a cs file with very simple code:

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:

cd c:\pst
Add-Type -Path "2.cs" -ReferencedAssemblies "Ionic.Zip.dll"
$var = [Helper]::GetNewFile("aaa")

When I do this it gives me:

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:

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?

Upvotes: 7

Views: 10913

Answers (2)

jon Z
jon Z

Reputation: 16616

In Powershell v3 CTP1 you can resolve the full path (fullname) of your zip library and reference that:

$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

Upvotes: 8

CB.
CB.

Reputation: 60908

You have to put your ionic.zip.dll file in the GAC then on powershell you can do this:

C:\ps> [System.Reflection.Assembly]::LoadWithPartialName("ionic.zip")
C:\ps> Add-Type -Path "2.cs" -ReferencedAssemblies "Ionic.Zip.dll"
C:\ps> $var = [Helper]::GetNewFile("aaa")
C:\ps> $var.name
aaa

Upvotes: 1

Related Questions