C# 从PowerShell 1.0访问自定义dll时出错
下面是我的powershell脚本C# 从PowerShell 1.0访问自定义dll时出错,c#,powershell,C#,Powershell,下面是我的powershell脚本 function hello() { $dllpath = "C:\\Documents and Settings\\raj\\pstest\\testlib.dll"; [Reflection.Assembly]::LoadFrom($dllpath) | out-null; $obj = New-Object testlib.TestClass; $obj.print(); } hello 下面是testlib中的Tes
function hello()
{
$dllpath = "C:\\Documents and Settings\\raj\\pstest\\testlib.dll";
[Reflection.Assembly]::LoadFrom($dllpath) | out-null;
$obj = New-Object testlib.TestClass;
$obj.print();
}
hello
下面是testlib中的TestClass,我正在尝试在powershell中访问它
using System;
namespace testlib
{
class TestClass
{
public TestClass()
{
}
public void print()
{
Console.WriteLine("Hi");
}
}
}
但我得到的错误如下所示
New-Object : Cannot find type [testlib.TestClass]: make sure the assembly conta
ining this type is loaded.
At C:\Documents and Settings\raj\pstest\script1.ps1:5 char:19
+ $obj = New-Object <<<< testlib.TestClass;
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentExcepti
on
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewOb
jectCommand
You cannot call a method on a null-valued expression.
At C:\Documents and Settings\raj\pstest\script1.ps1:6 char:12
+ $obj.print <<<< ();
+ CategoryInfo : InvalidOperation: (print:String) [], RuntimeExce
ption
+ FullyQualifiedErrorId : InvokeMethodOnNull
新对象:找不到类型[testlib.TestClass]:请确保程序集包含
正在加载此类型。
位于C:\Documents and Settings\raj\pstest\script1.ps1:5 char:19
+$obj=新建对象尝试以下操作:
[code]$dll=$($env:userprofile+'\pstest\testdll.dll')
然后尝试从库中调用一些静态方法。例如:哎呀,我的坏
$dll = $($env:userprofile + '\pstest\test.dll')
[void][Reflection.Assembly]::LoadFile($dll)
[MyNamespace.MyClass]::Print()
TestClass应为公共类:(
修改后,其工作当您从[Reflection.Assembly]中删除null时打印出的内容::LoadFrom($dllpath)| out null;
@Garath:请参见我的editException调用“LoadFrom”和“1”参数:“无法加载文件或汇编”file:///C:\“文档和设置\raj\pstest\testdll.dll”或其依赖项之一。系统找不到指定的文件。“出现错误。”
$dll = $($env:userprofile + '\pstest\test.dll')
[void][Reflection.Assembly]::LoadFile($dll)
[MyNamespace.MyClass]::Print()