如何在PowerShell中从C#转换此接口类型? [英] How can I do this interface type of cast from C# in PowerShell?

查看:41
本文介绍了如何在PowerShell中从C#转换此接口类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wuapi 编写一个脚本,用于扫描,下载和安装未连接到Internet的计算机的更新.我已经完成了大部分脚本,但是我需要能够对其他对象进行强制转换(我相信).

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. I have most of this script completed, but I need to be able to do a cast (I believe) to a different object.

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

I found code here that does exactly what I need, but in C#:
WUApiLib IUpdateInstaller2 yields Error; Some OS updates install others throw 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给了我一个负载库".错误类型.

The line ((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. 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?

推荐答案

如果问题是方法 CopyToCache 被实现为显式接口实现,则可以尝试

If the problem is that the method CopyToCache is implemented as an explicit interface implementation, maybe try

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

或类似的

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

这篇关于如何在PowerShell中从C#转换此接口类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆