2012-09-13 2 views
2

.net 콘솔 응용 프로그램 내에서 PowerShell Runspace에 어셈블리를로드하는 문제가 있습니다.문제 AssemblyConfigurationEntry를 사용하여 PowerShell에서 어셈블리로드

응용 프로그램을 실행할 때 "[Test.Libary.TestClass] 유형을 찾을 수 없습니다 :이 유형을 포함하는 어셈블리가로드되었는지 확인하십시오."라는 오류가 발생합니다.

어셈블리를 GAC에 설치하려고했지만 아무런 차이가없는 것으로 보입니다. AssemblyConfigurationEntry 클래스에 대한 많은 문서를 찾지 못해서 도움이된다.

콘솔 응용 프로그램 :

namespace PowerShellHost 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string assemblyPath = Path.GetFullPath("Test.Library.dll"); 

      RunspaceConfiguration config = RunspaceConfiguration.Create(); 

      var libraryAssembly = new AssemblyConfigurationEntry("Test.Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d7ac3058027aaf63", assemblyPath); 
     config.Assemblies.Append(libraryAssembly); 

      Runspace runspace = RunspaceFactory.CreateRunspace(config); 
      runspace.Open(); 

      PowerShell shell = PowerShell.Create(); 
      shell.Runspace = runspace; 

      shell.AddCommand("New-Object"); 
      shell.AddParameter("TypeName", "Test.Libary.TestClass"); 

      ICollection<PSObject> output = shell.Invoke(); 
     } 
    } 
} 

Test.Library.dll :

namespace Test.Library 
{ 
    public class TestClass 
    { 
     public string TestProperty { get; set; } 
    } 
} 

답변

2

당신은이 작업을 수행하는 스크립트에서 Add-Type를 호출 할 수 있습니다.

PowerShell shell = PowerShell.Create(); 

shell.AddScript(@" 
Add-Type -AssemblyName Test.Library 

$myObj = New-Object Test.Library.TestClass 
$myObj.TestProperty = 'foo' 
$myObj.TestPropery 
"); 

ICollection<PSObject> output = shell.Invoke(); 

DLL이 GAC에있는 경우 작동합니다. 그렇지 않으면 Add-Type 대신 -AssemblyName Test.Library을 호출하면 -Path c:\path\to\Test.Library.dll

+0

다른 컴퓨터에서 코드를 시도했지만 정상적으로 작동합니다. 당신은 또한 좋은 해결 방법입니다. 팁 고마워. –

관련 문제