using System.Diagnostics; using System.Reflection; using CertMgr.Core; using CertMgr.Core.Attributes; using CertMgr.Core.Jobs; using CertMgr.Core.Validation; using NUnit.Framework; namespace certmgrTest; public class PropertyDescriptorTest { [Test] public void First() { PropertyDescriptor? descriptor = GetProperty(typeof(TestSettings), nameof(TestSettings.StringItems)); if (descriptor == null) { throw new Exception(string.Format("property not found")); } IValueValidator? validator = descriptor.CustomValidator; } private static PropertyDescriptor? GetProperty(Type settingsType, string name) { PropertyDescriptor? result = null; foreach (PropertyInfo propertyInfo in settingsType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (propertyInfo.Name != name) { continue; } SettingAttribute? settingAttribute = propertyInfo.GetCustomAttribute(); if (settingAttribute is null) { continue; } result = new PropertyDescriptor(propertyInfo, settingAttribute, settingsType); break; } return result; } private sealed class TestSettings : JobSettings { [Setting("string-names", Validator = typeof(StringItemValidator))] public IReadOnlyCollection? StringItems { get; set; } } private sealed class StringItemValidator : IValueValidator, IValueValidator> { public StringItemValidator(string settingName) { ValueName = settingName; } public string ValueName { [DebuggerStepThrough] get; } Task IValueValidator.ValidateAsync(string? settingValue, CancellationToken cancellationToken) { return Task.FromResult((IValidationResult)new ValidationResult(ValueName, true, "OK")); } Task IValueValidator.ValidateAsync(object? value, CancellationToken cancellationToken) { return Task.FromResult((IValidationResult)new ValidationResult(ValueName, true, "OK")); } Task IValueValidator>.ValidateAsync(IEnumerable? settingValue, CancellationToken cancellationToken) { throw new NotImplementedException(); } } }