blob: 81f29c96c2ea64d3da5fd989a66d0339ee766efb (
plain)
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
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Commands;
namespace Tango.FSE.Procedures
{
public class DebugNode : ExtendedObject
{
private Object _originalValue;
public String Name { get; set; }
public Object ParentObject { get; set; }
private Object _value;
public Object Value
{
get { return _value; }
set
{
_value = value;
if (_originalValue == null)
{
_originalValue = _value;
}
DisplayValue = GetDisplayValue(_value);
RaisePropertyChangedAuto();
}
}
public bool IsSimpleValue { get; set; }
public String Type { get; set; }
public PropertyInfo PropertyInfo { get; set; }
public FieldInfo FieldInfo { get; set; }
public Type MemberType
{
get
{
if (PropertyInfo != null)
{
return PropertyInfo.PropertyType;
}
else if (FieldInfo != null)
{
return FieldInfo.FieldType;
}
else
{
return Value.GetType();
}
}
}
private bool _isEdited;
public bool IsEdited
{
get { return _isEdited; }
set { _isEdited = value; RaisePropertyChangedAuto(); }
}
public RelayCommand UpdateValueCommand { get; set; }
public bool IsEditable
{
get
{
if (IsSimpleValue)
{
return false; //Sorry can't update simple script symbols as they are passed to me by value...
}
if (MemberType != null && MemberType.IsValueTypeOrString() && ((PropertyInfo != null && PropertyInfo.SetMethod != null) || FieldInfo != null))
{
return true;
}
return false;
}
}
private Object _displayValue;
public Object DisplayValue
{
get { return _displayValue; }
set { _displayValue = value; RaisePropertyChangedAuto(); }
}
public List<DebugNode> Nodes
{
get
{
return GetChildNodes();
}
}
public DebugNode()
{
UpdateValueCommand = new RelayCommand(UpdateValue);
}
private void UpdateValue()
{
try
{
if (IsSimpleValue && Value != null)
{
_value = Convert.ChangeType(DisplayValue, Value.GetType());
IsEdited = _originalValue.ToStringSafe() != Value.ToStringSafe();
}
else if (IsEditable && ((PropertyInfo != null && PropertyInfo.SetMethod != null) || FieldInfo != null))
{
if (PropertyInfo != null)
{
_value = Convert.ChangeType(DisplayValue, PropertyInfo.PropertyType);
PropertyInfo.SetValue(ParentObject, Value);
IsEdited = _originalValue.ToStringSafe() != Value.ToStringSafe();
}
else if (FieldInfo != null)
{
_value = Convert.ChangeType(DisplayValue, FieldInfo.FieldType);
FieldInfo.SetValue(ParentObject, Value);
IsEdited = _originalValue.ToStringSafe() != Value.ToStringSafe();
}
}
}
catch { }
DisplayValue = GetDisplayValue(Value);
}
private List<DebugNode> GetChildNodes()
{
List<DebugNode> childNodes = new List<DebugNode>();
if (Value == null) return childNodes;
var type = Value.GetType();
if (type.IsValueTypeOrString())
{
return childNodes;
}
if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(String))
{
List<Object> list = (Value as IEnumerable).Cast<Object>().ToList();
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
DebugNode listNode = new DebugNode();
listNode.Name = $"[{i}]";
listNode.Value = item;
listNode.Type = GetFriendlyName(item.GetType());
childNodes.Add(listNode);
}
}
else
{
foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
DebugNode propNode = new DebugNode();
propNode.Type = GetFriendlyName(prop.PropertyType);
propNode.Name = prop.Name;
propNode.Value = prop.GetValue(Value);
propNode.PropertyInfo = prop;
propNode.ParentObject = Value;
childNodes.Add(propNode);
}
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
{
DebugNode propNode = new DebugNode();
propNode.Type = GetFriendlyName(field.FieldType);
propNode.Name = field.Name;
propNode.Value = field.GetValue(Value);
propNode.FieldInfo = field;
propNode.ParentObject = Value;
childNodes.Add(propNode);
}
}
return childNodes;
}
public static DebugNode CreateNode(String name, Object obj)
{
DebugNode node = new DebugNode();
node.Name = name;
node.Value = obj;
node.Type = GetFriendlyName(obj.GetType());
if (obj.GetType().IsValueTypeOrString())
{
node.IsSimpleValue = true;
}
return node;
}
private static String GetFriendlyName(Type type)
{
if (type == typeof(int))
return "int";
else if (type == typeof(short))
return "short";
else if (type == typeof(byte))
return "byte";
else if (type == typeof(bool))
return "bool";
else if (type == typeof(long))
return "long";
else if (type == typeof(float))
return "float";
else if (type == typeof(double))
return "double";
else if (type == typeof(decimal))
return "decimal";
else if (type == typeof(string))
return "string";
else if (type.IsGenericType)
return type.Name.Split('`')[0] + "<" + string.Join(", ", type.GetGenericArguments().Select(x => GetFriendlyName(x)).ToArray()) + ">";
else
return type.Name;
}
private static Object GetDisplayValue(Object value)
{
if (value == null)
{
return "null";
}
else if (value.GetType().IsValueTypeOrString())
{
return value;
}
else if (typeof(IEnumerable).IsAssignableFrom(value.GetType()))
{
List<Object> list = (value as IEnumerable).Cast<Object>().ToList();
return $"Count = {list.Count}";
}
return value.ToStringSafe();
}
}
}
|