Powershell CmdLet 和一次性字段/CA1001

标签 powershell

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/

相关文章:

powershell - 在什么情况下设计了Invoke-Expression?

Powershell v2::加载 COM 互操作 DLL

.net - 用于 .NET 开发的命令行(或等效)工具

javascript - 在 Powershell 中获取 JavaScript 结果

iis - 如何解释使用 powershell 配置的 BindingInformation

ruby - 如何使用 Chef 脚本安装 Windows 服务

parsing - 从 CSV 中删除行

json - 将部分json查询结果存储在powershell中的变量中

powershell - 将包含美元符号的字符串传递给 -Replace 变量

.net - Powershell:调用 .NET 函数总是需要括号吗?