다음은 내 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
다음은 powershell에서 액세스를 시도하는 testlib의 TestClass입니다.
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
추가 유형 cmddlet을 사용해 보았지만 동일한 응답을 제공합니다. dll이 powershell에 제대로로드되고 있다고 생각하지만 TestClass의 개체를 인스턴스화 할 수 없습니다. 내가 뭘 잘못하고 있는지 알려주세요.
내가 out-null을 제거하면 다음이 출력됩니다.
GAC Version Location
--- ------- --------
False v2.0.50727 C:\Documents and Settings\553566\pstest\testlib.dll
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
죄송합니다 .........
TestClass는 공용이어야합니다.
그 수정 후 작동
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다