PowerTip of the Day-Make Function Parameters Mandatory

原文地址:http://app.en25.com/e/es.aspx?s=1403&e=5441&elq=3d32771b868b437aa97567a6a50b5563

原文:

Mandatory parameters are special because if you do not submit it, PowerShell will automatically ask for it. You can use mandatory parameters in your own functions as well. You should simply mark a function as mandatory by adding the appropriate [Parameter()] attribute like so:

function Test-Me {
   
param(
          [
Parameter(Mandatory=$true)]
          
$name
    )
   
"You entered $name."
}

Test-Me

The parameter attributes used in this tip were introduced with PowerShell v2.

 

 

翻译:

Mandatory参数是很特殊的因为这种参数是在你不提交的时候,PowerShell会自动让你输入的。同样可以在你自己的函数里使用Mandatory参数。只需要用标记[Parameter()]属性标记在方法前面就可以了,比如:

function Test-Me {
   
param(
          [
Parameter(Mandatory=$true)]
          
$name
    )
   
"You entered $name."
}

Test-Me

这种参数属性的使用在PowerShell v2中就已经被介绍过了。

 

 

笔记:

Mandatory此处对应中文的意思不是很清楚,强迫?义务?强制?

最后一句话看得不是很懂。

posted @   哥本哈士奇(aspnetx)  阅读(384)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2009-07-08 Silverlight 3关于技术以外的一些个人预测
2009-07-08 Visifire 2.2.3 beta 5中CandleStick的一个Bug
点击右上角即可分享
微信分享提示