我在将程序集加载到 .net 控制台应用程序中的 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; }
}
}