using System.Collections;
using Microsoft.Extensions.Options;
using System.ComponentModel.DataAnnotations;
public class ContainerApiOptions
public int MaxUserDataLength { get; set; }
public interface IInt32Accessor
public class MaxUserDataLengthConfigAccessor : IInt32Accessor
private readonly ContainerApiOptions options;
public MaxUserDataLengthConfigAccessor(IOptions<ContainerApiOptions> options)
this.options = options.Value;
return options.MaxUserDataLength;
public class CustomMaxLengthAttribute : ValidationAttribute
private readonly Type intAccessorType;
public CustomMaxLengthAttribute(Type intAccessorType)
this.intAccessorType = intAccessorType;
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
var intAccessor = (IInt32Accessor)validationContext.GetService(intAccessorType);
var maxLength = intAccessor.GetValue();
return ValidationResult.Success;
var length = value is string str
: value is ICollection collection
return ValidationResult.Success;
return new ValidationResult("oups");
public static void Main()
Console.WriteLine("Hello World");