-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathDevToolsDomainEntityBase.cs
More file actions
237 lines (183 loc) · 8.23 KB
/
DevToolsDomainEntityBase.cs
File metadata and controls
237 lines (183 loc) · 8.23 KB
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
// Copyright © 2020 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
namespace CefSharp.DevTools
{
/// <summary>
/// Common Base class for DevTools Domain Model classes
/// </summary>
[DataContract]
public abstract class DevToolsDomainEntityBase
{
#if NETCOREAPP
internal static string EnumToString(Enum e)
{
var memberInfo = e.GetType().GetMember(e.ToString()).FirstOrDefault();
var enumMemberAttribute = (System.Text.Json.Serialization.JsonPropertyNameAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(System.Text.Json.Serialization.JsonPropertyNameAttribute), false);
return enumMemberAttribute.Name;
}
internal static string EnumToString(Array enumArray)
{
var returnValue = "[";
foreach (var e in enumArray)
{
var memberInfo = e.GetType().GetMember(e.ToString()).FirstOrDefault();
var enumMemberAttribute = (System.Text.Json.Serialization.JsonPropertyNameAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(System.Text.Json.Serialization.JsonPropertyNameAttribute), false);
returnValue += enumMemberAttribute.Name + ",";
}
returnValue += returnValue.Substring(0, returnValue.Length - 1) + "]";
return returnValue;
}
#else
internal static object StringToEnum(Type enumType, string input)
{
if (enumType.IsArray)
{
if (string.IsNullOrEmpty(input) || input == "[]" || input == "[ ]")
{
return null;
//return Array.CreateInstance(enumType.GetElementType(), 0);
}
var values = input.Substring(1, input.Length - 2).Split(',');
var returnValues = Array.CreateInstance(enumType.GetElementType(), values.Length);
for (int i = 0; i < values.Length; i++)
{
var str = values[i].Trim('\r', '\n', '"', ' ');
var enumVal = StringToEnumInternal(enumType.GetElementType(), str);
returnValues.SetValue(enumVal, i);
}
return returnValues;
}
if (enumType.IsGenericType && enumType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if (string.IsNullOrEmpty(input))
{
return null;
}
enumType = Nullable.GetUnderlyingType(enumType);
}
return StringToEnumInternal(enumType, input);
}
private static object StringToEnumInternal(Type enumType, string input)
{
foreach (var name in Enum.GetNames(enumType))
{
var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
if (enumMemberAttribute.Value == input)
{
return Enum.Parse(enumType, name);
}
}
return (Enum.GetValues(enumType).GetValue(0));
}
internal static string EnumToString(Enum e)
{
var memberInfo = e.GetType().GetMember(e.ToString()).FirstOrDefault();
var enumMemberAttribute = (EnumMemberAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(EnumMemberAttribute), false);
return enumMemberAttribute.Value;
}
internal static string EnumToString(Array enumArray)
{
var returnValue = "[";
foreach (var e in enumArray)
{
var memberInfo = e.GetType().GetMember(e.ToString()).FirstOrDefault();
var enumMemberAttribute = (EnumMemberAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(EnumMemberAttribute), false);
returnValue += enumMemberAttribute.Value + ",";
}
returnValue += returnValue.Substring(0, returnValue.Length - 1) + "]";
return returnValue;
}
#endif
#if NETCOREAPP
public IDictionary<string, object> ToDictionary()
{
var dict = new Dictionary<string, object>();
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in properties)
{
var propertyAttribute = (System.Text.Json.Serialization.JsonPropertyNameAttribute)Attribute.GetCustomAttribute(prop, typeof(System.Text.Json.Serialization.JsonPropertyNameAttribute), false);
//Only add members that have JsonPropertyNameAttribute
if (propertyAttribute == null)
{
continue;
}
var propertyName = propertyAttribute.Name;
var propertyRequired = Attribute.IsDefined(prop, typeof(System.Diagnostics.CodeAnalysis.DisallowNullAttribute));
var propertyValue = prop.GetValue(this);
if (propertyRequired && propertyValue == null)
{
throw new DevToolsClientException(prop.Name + " is required");
}
//Not required and value null, don't add to dictionary
if (propertyValue == null)
{
continue;
}
var propertyValueType = propertyValue.GetType();
if (typeof(DevToolsDomainEntityBase).IsAssignableFrom(propertyValueType))
{
propertyValue = ((DevToolsDomainEntityBase)(propertyValue)).ToDictionary();
}
else if (propertyValueType.IsEnum)
{
propertyValue = EnumToString((Enum)propertyValue);
}
else if(propertyValueType.IsGenericType)
{
var nullableType = Nullable.GetUnderlyingType(propertyValueType);
if(nullableType != null && nullableType.IsEnum)
{
propertyValue = EnumToString((Enum)propertyValue);
}
}
else if(propertyValueType.IsArray && propertyValueType.GetElementType().IsEnum)
{
propertyValue = EnumToString((Array)propertyValue);
}
dict.Add(propertyName, propertyValue);
}
return dict;
}
#else
public IDictionary<string, object> ToDictionary()
{
var dict = new Dictionary<string, object>();
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var prop in properties)
{
var dataMemberAttribute = (DataMemberAttribute)Attribute.GetCustomAttribute(prop, typeof(DataMemberAttribute), false);
//Only add members that have DataMemberAttribute
if (dataMemberAttribute == null)
{
continue;
}
var propertyName = dataMemberAttribute.Name;
var propertyRequired = dataMemberAttribute.IsRequired;
var propertyValue = prop.GetValue(this);
if (propertyRequired && propertyValue == null)
{
throw new DevToolsClientException(prop.Name + " is required");
}
//Not required and value null, don't add to dictionary
if (propertyValue == null)
{
continue;
}
var propertyValueType = propertyValue.GetType();
if (typeof(DevToolsDomainEntityBase).IsAssignableFrom(propertyValueType))
{
propertyValue = ((DevToolsDomainEntityBase)(propertyValue)).ToDictionary();
}
dict.Add(propertyName, propertyValue);
}
return dict;
}
#endif
}
}