alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using OpenTap.Addin.Entity;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Dynamic;
 
namespace OpenTap.Addin
{
    public class VariableConte : DynamicObject
    {
        private static readonly char SPLIT_MARK = '.';
 
        private readonly ConcurrentDictionary<string, RuntimeVariable> _____Variables_____;
 
 
        private readonly string _____name_____;
 
        public VariableConte(string name, ConcurrentDictionary<string, RuntimeVariable> variables)
        {
            _____name_____ = name;
            _____Variables_____ = variables;
        }
 
        public override bool TryGetMember(GetMemberBinder binder, out dynamic result)
        {
            result = null;
            string name = binder.Name;
            var type = binder.ReturnType;
 
            if (_____Variables_____.TryGetValue(name, out var variable))
            {
                if (type == typeof(RuntimeVariable))
                {
                    result = variable;
                }
                else
                {
                    result = variable;//.data;
                }
                return true;
            }
            result = new NullVariable($"{this._____name_____}.{name}");
            return true;
        }
 
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            string name = binder.Name;
            if (_____Variables_____.TryGetValue(name, out var variable))
            {
                if (value is RuntimeVariable rv)
                {
                    // 引用传递
                    _____Variables_____[name] = rv;
                }
                else
                {
                    variable.__data___ = NumberData.Format(value);
                }
                return true;
            }
            else
            {
                if (value is RuntimeVariable rv)
                {
                    // 引用传递
                    _____Variables_____[name] = rv;
                }
                else
                {
                    var v = new RuntimeVariable(null, name, NumberData.Format(value));
                    _____Variables_____[name] = v;
                }
                
                return true;
            }
        }
 
        public void Merge(VariableConte source)
        {
            if (source?._____Variables_____ != null)
            {
                foreach (var key in source._____Variables_____.Keys)
                {
                    _____Variables_____[key] = source._____Variables_____[key];
                }
            }
        }
 
        public void Merge(ConcurrentDictionary<string, RuntimeVariable> Variables)
        {
            if (Variables != null)
            {
                foreach (var key in Variables.Keys)
                {
                    this._____Variables_____[key] = Variables[key];
                }
            }
        }
 
        public static VariableConte FromTestVariables(string name, IEnumerable<TestVariable> variables)
        {
            var runtimeVariablePool = new ConcurrentDictionary<string, RuntimeVariable>();
            foreach (var data in variables)
            {
                RuntimeVariable runTime = data.ToRuntime(null);
                if (runTime is null) continue;
                runtimeVariablePool[runTime.__name___] = runTime;
            }
            return new VariableConte(name, runtimeVariablePool);
        }
    }
}