diff --git a/src/Blazilla/FluentValidator.cs b/src/Blazilla/FluentValidator.cs index 6652093..921dead 100644 --- a/src/Blazilla/FluentValidator.cs +++ b/src/Blazilla/FluentValidator.cs @@ -469,40 +469,21 @@ void IDisposable.Dispose() /// - /// Creates a compiled delegate factory for creating ValidationContext instances of the specified type. - /// This avoids the performance overhead of using Activator.CreateInstance with reflection. + /// Creates a factory for creating ValidationContext instances of the specified type. + /// Uses reflection instead of expression compilation so the component remains safe in iOS AOT-only mode. /// /// The model type to create a factory for. - /// A compiled delegate that creates ValidationContext instances. + /// A delegate that creates ValidationContext instances. private static Func CreateContextFactory(Type modelType) { var contextType = typeof(ValidationContext<>).MakeGenericType(modelType); - // Get the constructor that takes (T instance, PropertyChain propertyChain, IValidatorSelector validatorSelector) - var constructor = contextType.GetConstructor([modelType, typeof(PropertyChain), typeof(IValidatorSelector)]) - ?? throw new InvalidOperationException($"Could not find appropriate constructor for {contextType}"); - - // Create expression parameters - var instanceParam = Expression.Parameter(typeof(object), "instance"); - var propertyChainParam = Expression.Parameter(typeof(PropertyChain), "propertyChain"); - var selectorParam = Expression.Parameter(typeof(IValidatorSelector), "selector"); - - // Convert the instance parameter to the correct model type - var typedInstance = Expression.Convert(instanceParam, modelType); - - // Create the constructor call expression - var constructorCall = Expression.New(constructor, typedInstance, propertyChainParam, selectorParam); - - // Convert the result to IValidationContext - var convertedResult = Expression.Convert(constructorCall, typeof(IValidationContext)); - - // Compile the expression into a delegate - var lambda = Expression.Lambda>( - convertedResult, - instanceParam, - propertyChainParam, - selectorParam); + return (instance, propertyChain, selector) => + { + var context = Activator.CreateInstance(contextType, instance, propertyChain, selector); - return lambda.Compile(); + return context as IValidationContext + ?? throw new InvalidOperationException($"Could not create validation context for {modelType}"); + }; } }