繁体   English   中英

如何在 PowerShell 中从 C# 执行这种接口类型的转换?

[英]How can I do this interface type of cast from C# in PowerShell?

我正在使用wuapi编写一个脚本,该脚本为未连接到 Internet 的机器扫描、下载和安装更新。 我已经完成了这个脚本的大部分内容,但是我需要能够对不同的对象进行强制转换(我相信)。

我在这里找到了完全符合我需要的代码,但在 C# 中:
WUApiLib IUpdateInstaller2 产生错误; 一些操作系统更新安装其他人会抛出 HResult -2145124318

这是代码的样子:

var collection = new UpdateCollection();
IList<string> updateFiles = Directory.GetFiles(updateFolder);
var fileCollection = new StringCollection();

foreach (var file in updateFiles)
    fileCollection.Add(file);

//Error happens here on certain updates. Not all.

/** THIS LINE BELOW IS THE ONE I NEED **/
((IUpdate2)update.BundledUpdates[0]).CopyToCache(fileCollection);
collection.Add(update);
return collection;

((IUpdate2)update.BundledUpdates[0]).CopyToCache(fileCollection); 返回一个IUpdate对象,但为了使用CopyToCache函数,它需要是一个IUpdate2对象。 我似乎无法弄清楚如何让这部分工作,因为 powershell 给了我一个“加载库”类型的错误。

有谁知道我如何在 PowerShell 中做到这一点?

如果问题是方法CopyToCache是作为显式接口实现来实现的,不妨试试

[IUpdate2].GetMethod("CopyToCache").Invoke($update.BundledUpdates[0], @($fileCollection))

或类似。

请参阅如何从 PowerShell 调用显式实现的接口方法?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM