无法加载文件或程序集

nhr*_*bin 5 powershell powershell-2.0

在我的power shell脚本中,我正在加载一个自定义程序集,然后通过实例化该程序集的类New-Object.

Assembly.LoadFile()执行成功,但New-Object语句给出了波纹管异常.

New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of i
ts dependencies. The system cannot find the file specified."
Run Code Online (Sandbox Code Playgroud)

脚本:

[System.Reflection.Assembly]::LoadFile("MyAssembly.dll")
$a=New-Object MyAssembly.MyClass -ArgumentList "arg1"
Run Code Online (Sandbox Code Playgroud)

此自定义程序集仅引用以下程序集

System
System.Core
System.Runtime.Serialization
System.Xml.Linq
System.Data
System.Xml
Run Code Online (Sandbox Code Playgroud)

我尝试显式加载System.Runtime.Serialization dll,如下所示.但同样的例外

[System.Reflection.Assembly]::Load("System.Runtime.Serialization, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
Run Code Online (Sandbox Code Playgroud)

任何的想法?

Eri*_*ris 6

不要用LoadFile.由于您使用的是Powershell V2,因此首选方法是Add-Type

Add-Type -AssemblyName "MyLibrary.dll" 
Run Code Online (Sandbox Code Playgroud)

http://www.dougfinke.com/blog/index.php/2010/08/29/how-to-load-net-assemblies-in-a-powershell-session/列出了可用的不同方法将库导入当前的shell运行空间.

http://technet.microsoft.com/en-us/library/hh849914.aspx是关于Add-Type的technet文档,其示例包括如何在加载的库中使用静态方法