简体   繁体   English

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

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

I'm using the wuapi to write a script that scans, downloads, and installs updates for a machine that is not connected to the Internet.我正在使用wuapi编写一个脚本,该脚本为未连接到 Internet 的机器扫描、下载和安装更新。 I have most of this script completed, but I need to be able to do a cast (I believe) to a different object.我已经完成了这个脚本的大部分内容,但是我需要能够对不同的对象进行强制转换(我相信)。

I found code here that does exactly what I need, but in C#:我在这里找到了完全符合我需要的代码,但在 C# 中:
WUApiLib IUpdateInstaller2 yields Error; WUApiLib IUpdateInstaller2 产生错误; Some OS updates install others throw HResult -2145124318 一些操作系统更新安装其他人会抛出 HResult -2145124318

And here is what the code looks like:这是代码的样子:

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;

The line ((IUpdate2)update.BundledUpdates[0]).CopyToCache(fileCollection);((IUpdate2)update.BundledUpdates[0]).CopyToCache(fileCollection); returns an IUpdate object, but in order to use the CopyToCache function, it needs to be an IUpdate2 object.返回一个IUpdate对象,但为了使用CopyToCache函数,它需要是一个IUpdate2对象。 I just can't seem to figure out how to get this part working as powershell gives me a "load library" type of error.我似乎无法弄清楚如何让这部分工作,因为 powershell 给了我一个“加载库”类型的错误。

Does anyone know how I can do this in PowerShell?有谁知道我如何在 PowerShell 中做到这一点?

If the problem is that the method CopyToCache is implemented as an explicit interface implementation, maybe try如果问题是方法CopyToCache是作为显式接口实现来实现的,不妨试试

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

or similar.或类似。

See How can I call explicitly implemented interface method from PowerShell?请参阅如何从 PowerShell 调用显式实现的接口方法?

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

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