var slide = document.getElementById('slide');
if( slide ){
// .. your code
}
正如我们所看到的,我正在加载程序集,然后使用反射来创建目录的zip。但是当这个运行时,我收到
的错误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方法的相同内容就可以了。那么为什么不能在类中使用反射?
答案 0 :(得分:2)
IIRC类方法是预编译的,因此后期绑定不会使用[type]语法。我想我们需要手动调用ZipFile的方法:
class foo {
static hidden [Reflection.Assembly]$FS
static hidden [Reflection.TypeInfo]$ZipFile
static hidden [Reflection.MethodInfo]$CreateFromDirectory
[void] Zip() {
if (![foo]::FS) {
$assemblyName = 'System.IO.Compression.FileSystem'
[foo]::FS = [Reflection.Assembly]::LoadWithPartialName($assemblyName)
[foo]::ZipFile = [foo]::FS.GetType('System.IO.Compression.ZipFile')
[foo]::CreateFromDirectory = [foo]::ZipFile.GetMethod('CreateFromDirectory',
[type[]]([string], [string], [IO.Compression.CompressionLevel], [bool]))
}
$includeBaseDirectory = $false
$compressionLevel = [IO.Compression.CompressionLevel]::Optimal
[foo]::CreateFromDirectory.Invoke([foo]::ZipFile,
@('c:\test', 'c:\test.zip', $compressionLevel, $includeBaseDirectory))
}
}
$f = [foo]::new()
$f.Zip()