Systemi.ComponentModel.AnnotationsExecute.NET 2.2.9

dotnet add package Systemi.ComponentModel.AnnotationsExecute.NET --version 2.2.9
                    
NuGet\Install-Package Systemi.ComponentModel.AnnotationsExecute.NET -Version 2.2.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Systemi.ComponentModel.AnnotationsExecute.NET" Version="2.2.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Systemi.ComponentModel.AnnotationsExecute.NET" Version="2.2.9" />
                    
Directory.Packages.props
<PackageReference Include="Systemi.ComponentModel.AnnotationsExecute.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Systemi.ComponentModel.AnnotationsExecute.NET --version 2.2.9
                    
#r "nuget: Systemi.ComponentModel.AnnotationsExecute.NET, 2.2.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Systemi.ComponentModel.AnnotationsExecute.NET@2.2.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Systemi.ComponentModel.AnnotationsExecute.NET&version=2.2.9
                    
Install as a Cake Addin
#tool nuget:?package=Systemi.ComponentModel.AnnotationsExecute.NET&version=2.2.9
                    
Install as a Cake Tool

System.ComponentModel.AnnotationsEx

Provides extension methods for System.ComponentModel.DataAnnotations attributes, adding support for custom validation logic and enhanced error messages.

Provides attributes that are used to define metadata for objects used as data sources.

Commonly Used Types: System.ComponentModel.DataAnnotations.ValidationResult System.ComponentModel.DataAnnotations.IValidatableObject System.ComponentModel.DataAnnotations.ValidationAttribute System.ComponentModel.DataAnnotations.RequiredAttribute System.ComponentModel.DataAnnotations.StringLengthAttribute System.ComponentModel.DataAnnotations.DisplayAttribute System.ComponentModel.DataAnnotations.RegularExpressionAttribute System.ComponentModel.DataAnnotations.DataTypeAttribute System.ComponentModel.DataAnnotations.RangeAttribute System.ComponentModel.DataAnnotations.KeyAttribute

Usage

RequiredExAttribute

Makes a property required based on the value of another property.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class RequiredExAttribute : RequiredAttribute
{
    public string DependentProperty { get; set; }
    public object TargetValue { get; set; }

    public RequiredExAttribute(string dependentProperty, object targetValue)
    {
        DependentProperty = dependentProperty;
        TargetValue = targetValue;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (validationContext == null)
        {
            throw new ArgumentNullException(nameof(validationContext));
        }

        PropertyInfo dependentPropertyInfo = validationContext.ObjectType.GetProperty(DependentProperty);

        if (dependentPropertyInfo == null)
        {
            return new ValidationResult($"Dependent property '{DependentProperty}' not found on type '{validationContext.ObjectType.Name}'.");
        }

        object dependentPropertyValue = dependentPropertyInfo.GetValue(validationContext.ObjectInstance, null);

        if (Equals(dependentPropertyValue, TargetValue))
        {
            return base.IsValid(value, validationContext);
        }

        return ValidationResult.Success;
    }
}

▌StringLengthExAttribute

Ensures a string property's length does not exceed a maximum if the property is not null or empty.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class StringLengthExAttribute : StringLengthAttribute
{
    public StringLengthExAttribute(int maximumLength) : base(maximumLength)
    {
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null || string.IsNullOrEmpty(value.ToString()))
        {
            return ValidationResult.Success;
        }

        return base.IsValid(value, validationContext);
    }
}

▌Validate Extension Method

Provides a simple way to validate an object and get a list of validation errors.

public static class ValidationExtensions
{
    public static List<ValidationResult> Validate(this object obj)
    {
        var results = new List<ValidationResult>();
        var context = new ValidationContext(obj, serviceProvider: null, items: null);
        Validator.TryValidateObject(obj, context, results, validateAllProperties: true);
        return results;
    }
}

▌RegexExAttribute Validates a property against a regular expression pattern, but only if the value is not null or empty.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class RegexExAttribute : ValidationAttribute
{
    private readonly Regex _regex;

    public RegexExAttribute(string pattern)
    {
        _regex = new Regex(pattern);
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null || string.IsNullOrEmpty(value?.ToString()))
        {
            return ValidationResult.Success;
        }

        if (!_regex.IsMatch(value.ToString()))
        {
            return new ValidationResult(ErrorMessage);
        }

        return ValidationResult.Success;
    }
}
Product Compatible and additional computed target framework versions.
.NET Framework net is compatible.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.2.9 138 5/27/2025
2.2.8 141 5/27/2025
2.2.7 141 5/27/2025
2.2.6 140 5/27/2025
2.2.5 146 5/25/2025
2.2.4 138 5/25/2025
2.2.3.9 153 5/17/2025
2.2.3.8 173 5/16/2025
2.2.3.7 220 5/12/2025
2.2.3.6 205 5/12/2025
2.2.3.5 208 5/12/2025
2.2.3.4 190 5/11/2025
2.2.3.3 183 5/11/2025
2.2.3.2 186 5/11/2025
2.2.3.1 132 5/11/2025
2.2.3 127 5/11/2025
1.2.1 130 5/11/2025
1.2.0 132 5/11/2025
1.0.0 127 5/11/2025

Added extension methods for custom validation attributes