CmdLets 和一次性字段的可接受模式是什么?
FxCop 规则是:拥有一次性字段的类型应该是一次性的
但是除非 PowerShell 调用 dispose 方法......它不会真正帮助实现该模式。
到目前为止,我使用 Begin/EndProcessing 方法来设置和清除字段。
遗憾的是,我找不到任何关于 PowerShell 是否正确调用 Dispose 方法的文档。
最佳答案
在实现 Cmdlet(或 PSCmdlet)派生命令时,实现 IDisposable 并且 PowerShell 将在管道完成时为您处理您的命令。就这么简单。您是否看到与此相矛盾的行为?
更新,像 LetMeGoogleThatForYou 一样:
"...For this reason, a cmdlet that requires object cleanup should implement the complete IDisposable pattern, including a finalizer, so that the runtime can call both the System.Management.Automation.Cmdlet.EndProcessing and Dispose methods at the end of processing."
发件人:http://msdn.microsoft.com/en-us/library/windows/desktop/ms714463(v=vs.85).aspx
关于Powershell CmdLet 和一次性字段/CA1001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11288122/