2017-02-24 70 views
1

Powershell提供ValidateScript參數屬性來檢查給定屬性。例如:方法參數驗證屬性

function foo{ 
    Param(
     [ValidateScript({Test-Path $_ -PathType Leaf})] 
     [string] 
     $FilePath, 
    .... 

C#中是否有等價的可用於驗證方法的參數?所以,我可以在C#中像定義一個方法:在C#中

... 
public static string Foo (
    [Validate(p => p <1)] 
    int p) { 
    ... 
} 
+0

請分享一些這個參數爲PS做些什麼。 –

+0

c#中最接近的是[Code Contracts](https://msdn.microsoft.com/en-us/library/dd264808(v = vs.110).aspx)。 – Igor

+0

@Igor:我已經更新了我的問題。我對「一般」的參數檢查很感興趣。 – Moerwald

回答

0

檢查命名Dataannotation命名空間,這將驗證屬性與屬性的幫助。