我试图System.ComponentModel.DefaultValueAttribute添加到RuntimeDefinedParameter的AttributeCollection,但它简化版,工作..如何设置动态参数的默认值?
回答
System.Management.Automation.RuntimeDefinedParameter有“价值”的财产,所以我想我会用它来设置默认值。它有些作品(当我调试脚本时,我可以使用$ PSCmdlet.GetDynamicParameters()看到这个'默认'值),但是我没有在实际函数中访问它(它在$ pscmdlet.GetDynamicParameters()调用中执行'死亡'), 。
无论如何:当我指定值时,我的函数显示的是绑定值而不是默认值。
不知道它是否有帮助,并且TBH当参数为动态时,几乎看不到任何用例的默认值。很想看看你为什么需要它。 :)
这不正是你所需要的,但至少一个很好的解决方法(需要的PowerShell 3.0):
您可以使用$PSDefaultParameterValues
的所有功能,所有参数设置的默认值。请参阅https://technet.microsoft.com/en-us/library/hh847819.aspx或在about_Parameters_Default_Values
上使用PowerShell帮助。
作为鲍尔泰克建议,可以使用Value属性如下
DynamicParam {
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$colorlist = [System.Enum]::GetNames([System.Drawing.KnownColor])
$attributes = New-Object System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "__AllParameterSets"
$attributes.Mandatory = $false
# Background color
$validationset = New-Object -Type System.Management.Automation.ValidateSetAttribute -ArgumentList $colorlist
$collection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$collection.Add($attributes)
$collection.Add($validationset)
$background = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Background", [String], $collection)
$background.Value = "Transparent"
$newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$newparams.Add("Background", $background)
return $newparams
}
这里重要的线在代码看到的是$ background.Value =“透明”,其中$背景是RunTimeDefinedParameter。
对于那些好奇。我最初尝试将其用作属性,但ParameterAttributes中没有.Value可用。
Chrissy的例子可能是正确的做法,但我无法检索到默认值。当指定默认值时,该参数不存在于$ PSBoundParameters中。
我们应用的“解决方法”是将$ PSBoundParameter [“Background”]绑定到我们想要的默认值。 $PSBoundParameters["Background"] = "Transparent"
扩展克里希的例子:
DynamicParam {
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$colorlist = [System.Enum]::GetNames([System.Drawing.KnownColor])
$attributes = New-Object System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "__AllParameterSets"
$attributes.Mandatory = $false
# Background color
$validationset = New-Object -Type System.Management.Automation.ValidateSetAttribute -ArgumentList $colorlist
$collection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$collection.Add($attributes)
$collection.Add($validationset)
$background = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Background", [String], $collection)
$PSBoundParameters["Background"] = "Transparent"
$newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$newparams.Add("Background", $background)
return $newparams
}
这是我能够自己拥有默认值的动态参数的唯一方法。谢谢,托马斯! –
- 1. 动态设置数据集默认值
- 2. 设置动态值数据集参数/ Mainreport的默认值参数的iReport
- 3. 获取/设置参数的默认值动态
- 4. 动态设置Crystal Report Viewer参数默认值
- 5. 如何将[ScaffoldColumn(false)]设置为动态数据的默认值?
- 6. 如何为可选参数设置null的默认参数值?
- 7. 如何在struts-config.xml中为动作设置默认参数值?
- 8. 设置DROPDOWNLIST默认值动态
- 9. 如何将Cookie值设置为MapRoute参数的默认值?
- 10. 如何设置默认值
- 11. 如何设置默认值?
- 12. 如何设置maven数组参数的默认值?
- 13. 如何将PHP函数的参数设置为默认值
- 14. 设置默认参数fineUploader
- 15. 如何动态设置默认的ng-options值
- 16. Android的:如何设置默认值的参数变量
- 17. 如何设置默认值的参数proc是Tcl的
- 18. 如何设置默认的方法的参数值?
- 19. 无法将默认布尔参数设置为默认值
- 20. 如何为... params函数参数设置默认值?
- 21. 如何为命名函数参数设置默认值?
- 22. 如何为ServletContextPropertyPlaceholderConfigurer设置默认的上下文参数值?
- 23. 如何设置排序参数的默认值上一个ObjectDataSource
- 24. 如何为url helper方法的参数设置默认值?
- 25. 如何设置magento中参数的默认值?
- 26. 如何在参数列表中设置变量的默认值
- 27. 如何设置默认值在MVC模型动态
- 28. 如何为DropDownListFor使用asp.net动态设置默认值mvc4
- 29. 设置默认值[数字]
- 30. 如何设置UISegmentedControl的默认状态?
我需要的默认值,使参数是必需的。像'函数f($ p = @(抛出p是必需的)){}' – Jack128
'强制'不是'比'更好的选择吗? – BartekB
编号强制显示对话框(或命令行提示符),但我的脚本必须在没有人为操作的情况下工作 – Jack128