Exception Powershell 2.0运行时异常;无法加载文件或程序集

Exception Powershell 2.0运行时异常;无法加载文件或程序集,exception,powershell,runtime,Exception,Powershell,Runtime,我试图纠正的情况是,在编译C#代码时会找到DLL,但在执行C#代码时却找不到DLL。这是Powershell 2.0。我们的政策是不使用GAC。Powershell函数中的c#代码与此类似: function functionDef { [System.Reflection.Assembly]::LoadFrom("c:\myDir\func1.dll") [System.Reflection.Assembly]::LoadFrom("c:\myDir\func2.dll") $r

我试图纠正的情况是,在编译C#代码时会找到DLL,但在执行C#代码时却找不到DLL。这是Powershell 2.0。我们的政策是不使用GAC。Powershell函数中的c#代码与此类似:

function functionDef
{
  [System.Reflection.Assembly]::LoadFrom("c:\myDir\func1.dll")
  [System.Reflection.Assembly]::LoadFrom("c:\myDir\func2.dll")

  $ref = @("c:\myDir\func1.dll","c:\myDir\func2.dll")

  $cCode = @"
    using System;
    using func1;
    using func2;

    namespace serializedDef
    {
       public class defSerialization
       {

          public defSerialization () {}

   <# 
             method and properyty defs not included
          #>

          public double setSpec {
  set { computeDef(value)}
  get {return spefDef}
          }

   private double computeDef ( double value)
   {
            <# calls to methods in DLLS loaded above
             #>
   }
       }
    }
 "@
 add-type -ReferencedAssemblies $ref -TypeDefinition $cCode -passthru - Language CSharpVersion3 | out-null
}
调用函数时:

 $myObject.setSpec = 35.5
我得到一个错误:

Exception setting "setSpec": "Could not load  file or assmbly
'func1', Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0a7c34216660f47' or one of its dependencies. The system cannot find the file specified."
At line:1 char:11
+ $myObject. <<< setSpec = 35.5
  + CategogyInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifedErrorId  : PropertyAssignmentException
异常设置“setSpec”:“无法加载文件或组件”
“func1”、版本=1.0.0.0、区域性=中性、PublicKeyToken=f0a7c34216660f47”或其依赖项之一。系统找不到指定的文件。”
第1行字符:11

+$myObject .NET程序集不是从路径加载的。app base dir是PowerShell安装dir,您不想将程序集复制到那里。我建议挂起AppDomain.AssemblyResolve事件。当CLR找不到程序集时,将调用此事件,此时您可以提供相关程序集的完整路径。更多信息,请参见此处。

使用fusglow,我注意到错误为“绑定到本机映像程序集未成功”。使用“IL图像”。模糊不清地理解信息,但不知道如何实施必要的更改。
Exception setting "setSpec": "Could not load  file or assmbly
'func1', Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0a7c34216660f47' or one of its dependencies. The system cannot find the file specified."
At line:1 char:11
+ $myObject. <<< setSpec = 35.5
  + CategogyInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifedErrorId  : PropertyAssignmentException