alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
 
namespace OpenTap.Addin.Reader
{
    public abstract class IObjectReader
    {
        internal const string TypeAttrName = "type";
 
        public abstract object ReadObject(Type type, XElement element, Action<object> postAction);
    }
 
    public class DefaultObjectReader
    {
        static readonly Dictionary<Type, IObjectReader> SpecialReader = new Dictionary<Type, IObjectReader>();
 
        static DefaultObjectReader()
        {
            SpecialReader.Add(typeof(TestStepList), new TestStepListReader());
        }
 
        static bool tryConvertNumber(Type type, string valueString, out object value)
        {
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Byte:
                    {
                        byte oval;
                        bool ok = byte.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.SByte:
                    {
                        SByte oval;
                        bool ok = SByte.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.UInt16:
                    {
                        UInt16 oval;
                        bool ok = UInt16.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.UInt32:
                    {
                        UInt32 oval;
                        bool ok = UInt32.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.UInt64:
                    {
                        UInt64 oval;
                        bool ok = UInt64.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
 
                case TypeCode.Int16:
                    {
                        Int16 oval;
                        bool ok = Int16.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.Int32:
                    {
                        Int32 oval;
                        bool ok = Int32.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.Int64:
                    {
                        Int64 oval;
                        bool ok = Int64.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.Decimal:
                    {
                        value = BigFloat.Convert(valueString, CultureInfo.InvariantCulture).ConvertTo(typeof(decimal));
                        return true;
                    }
                case TypeCode.Double:
                    {
                        Double oval;
                        bool ok = Double.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                case TypeCode.Single:
                    {
                        Single oval;
                        bool ok = Single.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out oval);
                        value = oval;
                        return ok;
                    }
                default:
                    value = null;
                    return false;
            }
        }
 
        static bool readContentInternal(Type propType, Func<string> getvalueString, XElement elem, out object outvalue)
        {
 
            object value = null;
            bool ok;
 
            if (propType.IsEnum || propType.IsPrimitive || propType == typeof(string) || propType.IsValueType || propType == typeof(Type))
            {
                ok = true;
                string valueString = getvalueString()?.Trim();
 
                if (propType.IsEnum)
                {
                    if (!string.IsNullOrEmpty(valueString))
                    {
                        // legacy support: A flagged enum did not have ','s, but just spaces between.
                        if (!valueString.Contains(','))
                        {
                            var splitted = valueString.Split(' ');
                            value = Enum.Parse(propType, string.Join(",", splitted));
                        }
                        else
                            value = Enum.Parse(propType, valueString);
                    }
                }
 
                else if (propType == typeof(String) || propType == typeof(char))
                {
                    if (elem.HasElements)
                    {
                        // string contains Base64 if it has invalid XML chars.
                        // elem.Element("Base64") fails if the attribute "xmlns" is set on the document.
                        // In this case, "Base64" must be prepended with the value of xmlns in brackets.
                        // If the namespace is not set, ns.GetName("Base64") will just evaluate to "Base64"
                        var ns = elem.GetDefaultNamespace();
                        var encode = elem.Element(ns.GetName("Base64"));
                        if (encode != null)
                        {
                            try
                            {
                                value = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(encode.Value));
                            }
                            catch
                            {
                                value = encode.Value;
                            }
                        }
                    }
                    if (value == null)
                        value = valueString;
                    if (propType == typeof(char))
                        value = ((string)value)[0];
                }
                else if (propType == typeof(TimeSpan) || propType == typeof(TimeSpan?))
                {
                    value = TimeSpan.Parse(valueString, CultureInfo.InvariantCulture);
                }
                else if (propType == typeof(Guid) || propType == typeof(Guid?))
                {
                    value = Guid.Parse(valueString);
                }
                else if (propType == typeof(Type))
                {
                    value = PluginManager.LocateType(valueString.Split(',').First());
                }
                else if (tryConvertNumber(propType, valueString, out value))
                {
                    // Conversions done in tryConvertNumber
                }
                else if (typeof(IConvertible).IsAssignableFrom(propType))
                {
                    value = Convert.ChangeType(valueString, propType, CultureInfo.InvariantCulture);
                }
                else if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    // For nullable<T> we recurse focusing on the underlying non-nullable type.
                    if (valueString == null)
                    {
                        outvalue = null;
                        return true; // null is a valid value for nullable types.
                    }
 
                    return readContentInternal(Nullable.GetUnderlyingType(propType), getvalueString, elem, out outvalue);
                }
                else
                {
                    ok = false;
                }
            }
            else
            {
                ok = false;
            }
 
            outvalue = value;
            return ok;
        }
 
        public static object ReadObject(Type type, XElement element, Action<object> postAction)
        {
            //var timer = Stopwatch.StartNew();
            if (type == null || element == null)
                return null;
            object obj = null;
            if (SpecialReader.ContainsKey(type))
            {
                obj = SpecialReader[type].ReadObject(type, element, postAction);
                //TestPlanReader.Log.Debug(timer, $"Create {obj}");
                return obj;
            }
            else
            {
                var typeAttr = element.Attribute(IObjectReader.TypeAttrName);
                type = typeAttr == null ? type : Type.GetType(typeAttr.Value);
                if (type == null)
                {
                    var td = TypeData.GetTypeData(typeAttr.Value);
                    obj = td.CreateInstance();
                    type = obj.GetType();
                }
 
                if (!readContentInternal(type, () => element.Value, element, out obj))
                {
                    try
                    {
                        obj = obj ?? Activator.CreateInstance(type);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
 
                    if (typeof(IList).IsAssignableFrom(type) && type.IsGenericType)
                    {
                        var list = (IList)obj;
                        var eles = element.Elements().ToArray();
                        var values = new object[eles.Length];
 
                        Parallel.For(0, eles.Length, i => values[i] = ReadObject(type.GetGenericArguments().First(), eles[i], postAction));
 
                        //var values = element.Elements().AsParallel().AsOrdered().Select(childElemennt =>
                        //{
                        //    return ;
                        //}).ToArray();
 
                        foreach (var val in values)
                        {
                            list.Add(val);
                        }
 
                        //foreach (var childElemennt in element.Elements())
                        //{
                        //    var value = ReadObject(type.GetGenericArguments().First(), childElemennt, postAction);
                        //    list.Add(value);
                        //}
                    }
                    else
                    {
                        var props = PropertyInfoCache.GetProps(type);
 
                        foreach (var childElemennt in element.Elements())
                        {
                            string childName = XmlConvert.DecodeName(childElemennt.Name.LocalName);
                            if (!props.ContainsKey(childName))
                            {
                                continue;
                            }
                            var prop = props[childName];
                            var value = ReadObject(prop.PropertyType, childElemennt, postAction);
                            prop.SetValue(obj, value);
                        }
 
                        //List<Task> tasks = new List<Task>();
                        //foreach (var childElemennt in element.Elements())
                        //{
                        //    string childName = XmlConvert.DecodeName(childElemennt.Name.LocalName);
                        //    if (!props.ContainsKey(childName))
                        //    {
                        //        continue;
                        //    }
                        //    tasks.Add(Task.Run(() =>
                        //    {
                        //        
 
                        //    }));
                        //}
                        //Task.WaitAll(tasks.ToArray());
 
                        //var eles = element.Elements().ToArray();
                        //Parallel.For(0, eles.Length, i =>
                        //{
                        //    string childName = XmlConvert.DecodeName(eles[i].Name.LocalName);
                        //    if (!props.ContainsKey(childName))
                        //    {
                        //        return;
                        //    }
                        //    var prop = props[childName];
                        //    var value = ReadObject(prop.PropertyType, eles[i], postAction);
                        //    prop.SetValue(obj, value);
                        //});
                    }
                    //TestPlanReader.Log.Debug(timer, $"Create {obj}");
                }
                if (postAction != null) postAction(obj);
                return obj;
            }
        }
    }
}