1
晚上好, 我正在測試V5中的Powershell類,我無法在Powershell類中使用反射。以下例子:在Powershell類中使用反射
class PSHello{
[void] Zip(){
Add-Type -Assembly "System.IO.Compression.FileSystem"
$includeBaseDirectory = $false
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory('C:\test', 'c:\test.zip',$compressionLevel ,$includeBaseDirectory)
}
}
$f = [PSHello]::new()
$f.Zip()
我們可以看到,我正在加載程序集,然後使用反射來創建目錄的zip文件。這是運行然而,當我收到的
Unable to find type [System.IO.Compression.ZipFile].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
的錯誤,如果我現在運行它的工作原理類以外我郵編方法相同的內容。那麼爲什麼Reflection不能在課堂內像這樣使用?
強大有趣的,謝謝你的問題 – jladd
調用表達式的答案和解釋可能是一個簡單的解決方案... – wOxxOm