Table of Contents

Class AllowedValuesAttribute

Namespace
McMaster.Extensions.CommandLineUtils
Assembly
McMaster.Extensions.CommandLineUtils.dll

Specifies a set of allowed values and a comparer used to determine if a value is in that set.

By default, value comparison is case-sensitive. To ensure case matches exactly, set IgnoreCase to false.

[AttributeUsage(AttributeTargets.Property)]
public sealed class AllowedValuesAttribute : ValidationAttribute
Inheritance
object
Attribute
ValidationAttribute
AllowedValuesAttribute

Constructors

AllowedValuesAttribute(StringComparison, params string[])

Initializes an instance of AllowedValuesAttribute.

public AllowedValuesAttribute(StringComparison comparer, params string[] allowedValues)

Parameters

comparer StringComparison
allowedValues string[]

AllowedValuesAttribute(params string[])

Initializes an instance of AllowedValuesAttribute.

public AllowedValuesAttribute(params string[] allowedValues)

Parameters

allowedValues string[]

Properties

Comparer

The comparison method used.

public StringComparison Comparer { get; set; }

Property Value

StringComparison

IgnoreCase

Comparison between values and allowed values should ignore case.

public bool IgnoreCase { get; set; }

Property Value

bool

Methods

IsValid(object?, ValidationContext)

Validates the specified value with respect to the current validation attribute.

protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)

Parameters

value object

The value to validate.

validationContext ValidationContext

The context information about the validation operation.

Returns

ValidationResult

An instance of the ValidationResult class.

Exceptions

InvalidOperationException

The current attribute is malformed.

NotImplementedException

IsValid(object, ValidationContext) has not been implemented by a derived class.