2

Powershell offers a ValidateScript parameter attribute to check given attributes. Example:

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

Is there an equivalent available in C#, to validate parameters of a method? So that I can define a method in c# like:

...
public static string Foo (
   [Validate(p => p <1)]
   int p) {
   ...
}
Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • Please share some more light on what this param does for PS. – Nikhil Agrawal Feb 24 '17 at 15:09
  • The closest thing in c# are [Code Contracts](https://msdn.microsoft.com/en-us/library/dd264808(v=vs.110).aspx). – Igor Feb 24 '17 at 15:13
  • @Igor: I`ve update my question. I'm interested at parameter checking "just in general". – Moerwald Feb 24 '17 at 15:14
  • Yes, of cause. Here is a [link](https://msdn.microsoft.com/en-us/library/system.management.automation.validatescriptattribute%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396). Powershell is built on .NET Framework, so it can access all classes of standart library. Also, check [this answer](https://stackoverflow.com/questions/27908470/validatescript-parameterattribute-in-c-sharp-binary-powershell-module), maybe it can help you. – Backs Feb 24 '17 at 15:10

1 Answers1

0

Check namespace Dataannotation namespace in c# this will validate property with the help of attribute.

Sudhakar singh
  • 104
  • 2
  • 9