chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using OpenTap.Addin.Annotation;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
 
namespace OpenTap.Addin
{
    public enum TestVariableType
    {
        String = 0,
        Int = 1,
        Double = 2,
        Bool = 3,
        Container = 8,
        Refrence = 9
    }
 
    public class TestVariable : INotifyPropertyChanged
    {
        #region ...
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected void Set([CallerMemberName] string name = null)
        {
 
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
 
        protected void Set<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                Set(propertyName);
            }
        }
        #endregion
 
        private string name;
        [DataGridCol("名称", "*", false)]
        public string Name
        {
            get => name;
            set
            {
                Set(ref name, value);
            }
        }
 
        public bool IsArray { get; set; }
 
        private TestVariableType type;
        [DataGridCol("类型", "*", false)]
 
        public TestVariableType Type
        {
            get => type;
            set
            {
                Set(ref type, value);
            }
        }
 
        [DataGridCol("值", "*", false)]
        public string Value { get; set; }
 
        private ObservableCollection<TestVariable> children = new ObservableCollection<TestVariable>();
        public ObservableCollection<TestVariable> Children
        {
            get => children;
            set
            {
                Set(ref children, value);
            }
        }
 
        public object GetValue()
        {
            if (Type == TestVariableType.Int)
            {
                if (int.TryParse(Value, out int intValue))
                    return intValue;
            }
            else if (Type == TestVariableType.Double)
            {
                if (double.TryParse(Value, out double doubleValue))
                    return doubleValue;
            }
            else if (Type == TestVariableType.Bool)
            {
                if (bool.TryParse(Value, out bool boolValue))
                    return boolValue;
            }
            else if (Type == TestVariableType.String)
            {
                return Value;
            }
            return null;
        }
 
        public RuntimeVariable ToRuntime()
        {
            if (IsArray)
            {
                switch (Type)
                {
                    case TestVariableType.String:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(string[]),
                            TypeName = "string[]",
                            data = Children.Select(e => e.GetValue()).ToArray()
                        };
                    case TestVariableType.Int:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(int[]),
                            TypeName = "int[]",
                            data = Children.Select(e => e.GetValue()).ToArray()
                        };
                    case TestVariableType.Double:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(double[]),
                            TypeName = "double[]",
                            data = Children.Select(e => e.GetValue()).ToArray()
                        };
                    case TestVariableType.Bool:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(bool[]),
                            TypeName = "bool[]",
                            data = Children.Select(e => e.GetValue()).ToArray()
                        };
                    case TestVariableType.Container:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(object[]),
                            TypeName = "Container[]",
                            data = new VariableContext(new ConcurrentDictionary<string, RuntimeVariable>
                                (Children.Select(e => e.ToRuntime()).ToDictionary(v => v.Name, v => v)))
                        };
                }
            }
            else
            {
                switch (Type)
                {
                    case TestVariableType.String:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(string),
                            TypeName = "string",
                            data = Value
                        };
                    case TestVariableType.Int:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(int),
                            TypeName = "int",
                            data = int.Parse(Value)
                        };
                    case TestVariableType.Double:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(double),
                            TypeName = "double",
                            data = double.Parse(Value)
                        };
                    case TestVariableType.Bool:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(bool),
                            TypeName = "bool",
                            data = bool.Parse(Value)
                        };
                    case TestVariableType.Container:
                        return new RuntimeVariable
                        {
                            Name = Name,
                            Type = typeof(object[]),
                            TypeName = "object",
                            data = new VariableContext(new ConcurrentDictionary<string, RuntimeVariable>
                               (Children.Select(e => e.ToRuntime()).ToDictionary(v => v.Name, v => v)))
                        };
                }
            }
            return null;
        }
 
        public static string DefaultValue(TestVariableType type)
        {
            switch (type)
            {
                case TestVariableType.String:
                    return string.Empty;
                case TestVariableType.Int:
                    return "0";
                case TestVariableType.Double:
                    return "0";
                case TestVariableType.Bool:
                    return "False";
                default:
                    return null;
            }
        }
    }
}