2010-06-22 55 views
2

我從來沒有能夠獲得AllowEmptyString驗證屬性的工作。是否有一些技巧AllowEmptyString

此:

function Get-InputString(
    [parameter(mandatory=$true, position=0)][string][AllowEmptyString]$Str 
) 
{ 
    $Str 
} 

結果在此:

PS C:\> Get-InputString '' 
Unable to find type [AllowEmptyString]: make sure that the assembly containing this type is loaded. 
At line:2 char:71 
+  [parameter(mandatory=$true, position=0)][string][AllowEmptyString] <<<< $Str 
    + CategoryInfo   : InvalidOperation: (AllowEmptyString:String) [], RuntimeException 
    + FullyQualifiedErrorId : TypeNotFound 

回答

2

你錯過了該屬性的括號。看到http://technet.microsoft.com/en-us/library/dd347600.aspx

function Get-InputString 
{ 
    Param(
     [Parameter(Mandatory=$true, Position=0)] 
     [AllowEmptyString()] 
     [String] 
     $Str 
    ) 

    $Str 
} 
+0

花了我一分鐘看到這一點。而不是複製/粘貼你的repro,我輸入的屬性,並下意識地輸入的parens。對不起我的無用的評論。 – xcud 2010-06-22 19:58:50

+0

謝謝。如果他們在PowerShell幫助中的示例中有parens,那將會很不錯。 – OldFart 2010-06-22 19:59:08

+0

我正在尋找about_advanced_functions中的示例,並且我看到了parens - 你說的那個樣本在哪裏沒有parens? – x0n 2010-06-24 16:57:37