【问题标题】:How can I do this interface type of cast from C# in PowerShell?如何在 PowerShell 中从 C# 执行这种接口类型的转换?
【发布时间】:2013-10-15 10:52:13
【问题描述】:

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

我在这里找到了完全符合我需要的代码,但在 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 给了我一个“加载库”类型的错误。

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

【问题讨论】:

    标签: c# .net windows powershell casting


    【解决方案1】:

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

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

    或类似的。

    How can I call explicitly implemented interface method from PowerShell?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多