#nullable enable namespace Bts.DataStreamCore; using System; using System.Collections.Generic; using System.Linq; public class CompositeExtensionFunctionEvaluator : IExtensionFunctionEvaluator { readonly List evaluators = new(); public CompositeExtensionFunctionEvaluator(Type targetType) { TargetType = targetType; } public Type TargetType { get; } public IEnumerable SupportedFunctions => evaluators.SelectMany(e => e.SupportedFunctions); public void Add(IExtensionFunctionEvaluator evaluator) { if (evaluator.TargetType != TargetType) { throw new ArgumentException("TargetType does not match composite evaluator's TargetType", nameof(evaluator)); } evaluators.Add(evaluator); } public object? Evaluate(string functionId, object target, IEnumerable<(string? Name, int? Index, object? Value)> args) { var targetType = target.GetType(); foreach (var eval in evaluators) { if (eval.SupportedFunctions.Contains(functionId) && targetType.IsAssignableTo(eval.TargetType)) { return eval.Evaluate(functionId, target, args); } } return null; } }