我正在尝试释放XmlWriter对象:
try
{
[System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml')
}
finally
{
$writer.Dispose()
}
错误:
方法调用失败,因为[System.Xml.XmlWellFormedWriter]中没有名为'Dispose'的方法。
另一方面:
$writer -is [IDisposable]
# True
我应该做什么?
Dispose
是不正确的。他想要的Dispose
不是protected
,而是显式接口实现!显式接口实现在 PowerShell 中很难调用。但这个 hack 应该可以解决问题:[IDisposable].GetMethod("Dispose").Invoke($writer, @())
。 - Jeppe Stig Nielsen