And*_*huk 5 powershell powershell-2.0
我有一个非常简单的代码的cs文件:
using Ionic.Zip;
public static class Helper
{
public static ZipFile GetNewFile(string fileName)
{
return new ZipFile(fileName);
}
}
Run Code Online (Sandbox Code Playgroud)
它需要Ionic.Zip组装.我想将此类型添加到我的powershell中,如下所示:
cd c:\pst
Add-Type -Path "2.cs" -ReferencedAssemblies "Ionic.Zip.dll"
$var = [Helper]::GetNewFile("aaa")
Run Code Online (Sandbox Code Playgroud)
当我这样做时它给了我:
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)"
Run Code Online (Sandbox Code Playgroud)
它似乎已经在某个临时位置编译了程序集,并且在那里找不到Ionic.Zip.
但是,如果指定输出程序集然后添加此程序集,则它可以正常工作:
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
Run Code Online (Sandbox Code Playgroud)
所以我想知道是否有办法避免使用输出组件?
在Powershell v3 CTP1中,您可以解析zip库的完整路径(fullname)并引用:
$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
Run Code Online (Sandbox Code Playgroud)
您必须将 ionic.zip.dll 文件放入GAC 中 ,然后在 powershell 上您可以执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7128 次 |
| 最近记录: |