using System.Collections.Concurrent; using System.Collections.Generic; using System.Dynamic; using System.IO; using System.Numerics; namespace OpenTap.Addin { public class VariableContext : DynamicObject { private static readonly char SPLIT_MARK = '.'; private readonly ConcurrentDictionary Variables; public VariableContext(ConcurrentDictionary variables) { Variables = variables; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = null; string name = binder.Name; if (Variables.TryGetValue(name, out var variable)) { result = variable.data; return true; } return false; } public override bool TrySetMember(SetMemberBinder binder, object value) { string name = binder.Name; if (Variables.TryGetValue(name, out var variable)) { variable.data = value; return true; } else { var v = new RuntimeVariable { Name = name, data = value, Type = value.GetType(), TypeName = value.GetType().Name, }; Variables[name] = v; return true; } } public void Merge(VariableContext source) { if (source?.Variables != null) { foreach (var key in source.Variables.Keys) { Variables[key] = source.Variables[key]; } } } public void Merge(ConcurrentDictionary Variables) { if (Variables != null) { foreach (var key in Variables.Keys) { this.Variables[key] = Variables[key]; } } } public static VariableContext FromTestVariables(IEnumerable variables) { var runtimeVariablePool = new ConcurrentDictionary(); foreach (var data in variables) { RuntimeVariable runTime = data.ToRuntime(); if (runTime == null) continue; runtimeVariablePool[runTime.Name] = runTime; } return new VariableContext(runtimeVariablePool); } } }