-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDataRef.cs
77 lines (58 loc) · 1.59 KB
/
DataRef.cs
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
using System;
using System.Globalization;
namespace ExtPlaneNet
{
public abstract class DataRef
{
public readonly string Name;
public readonly float Accuracy;
public Exception Exception { get; set; }
protected DataRef(string name, float accuracy = 0.0f)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException("name");
Name = name;
Accuracy = accuracy;
}
public abstract void SetValue(object value);
public static Type ParseType(string rawType)
{
if (string.IsNullOrWhiteSpace(rawType))
throw new ArgumentNullException("rawType");
switch (rawType)
{
case "i":
return typeof(int);
case "f":
return typeof(float);
case "d":
return typeof(double);
case "ia":
return typeof(int[]);
case "fa":
return typeof(float[]);
default:
return typeof(string);
}
}
public static object ParseValue(string rawValue, Type type)
{
if (string.IsNullOrWhiteSpace(rawValue))
throw new ArgumentNullException("rawValue");
if (type == null)
throw new ArgumentNullException("type");
object value;
if (type.IsArray)
{
string[] elements = rawValue.Replace("[", string.Empty).Replace("]", string.Empty).Split(',');
Array array = Array.CreateInstance(type.GetElementType(), elements.Length);
for (int i = 0; i < array.Length; i++)
array.SetValue(Convert.ChangeType(elements[i], type.GetElementType(), CultureInfo.InvariantCulture), i);
value = array;
}
else
value = Convert.ChangeType(rawValue, type, CultureInfo.InvariantCulture);
return value;
}
}
}