5

在我的 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."

脚本:

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

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

System
System.Core
System.Runtime.Serialization
System.Xml.Linq
System.Data
System.Xml

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

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

任何的想法?

4

2 回答 2

6

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

Add-Type -AssemblyName "MyLibrary.dll" 

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 的技术网文档,其中包含如何在加载的库中使用静态方法的示例

于 2013-08-26T15:36:59.163 回答
0

当我尝试从 64 位 Windows Powershell ISE 调用 32 位 Oracle.DataAccess.dll 时遇到了这个问题。后来我在 Windows Powershell ISE (x86) 中使用了相同的代码,并且能够调用 Oracle.DataAccess.dll 文件。干杯!

于 2014-04-01T20:47:28.570 回答