-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeHelper.cs
More file actions
185 lines (159 loc) · 5.93 KB
/
Copy pathTypeHelper.cs
File metadata and controls
185 lines (159 loc) · 5.93 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
//
// Copyright (c) 2018, Intel Corporation. All rights reserved.
//
// Author: Cezary Rojewski <cezary.rojewski@intel.com>
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
namespace NUcmSerializer
{
internal static class TypeHelper
{
internal static bool IsSimpleType(this Type type)
{
return type.IsPrimitive || type.IsValueType || type == typeof(string);
}
internal static bool IsSimpleArrayType(this Type type)
{
return type.IsArray && IsSimpleType(type.GetElementType());
}
internal static bool IsSimpleTupleType(this Type type)
{
if (type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Tuple<,>))
{
var args = type.GenericTypeArguments;
foreach (var arg in args)
if (!IsSimpleType(arg))
return false;
return true;
}
return false;
}
internal static bool IsVendorArrayType(this Type type)
{
return type.IsArray && !IsSimpleType(type.GetElementType());
}
internal static TokenType GetTypeTokenType(this Type type)
{
if (type.IsSimpleType())
return TokenType.Element;
if (type.IsSimpleArrayType())
return TokenType.Array;
if (type.IsSimpleTupleType())
return TokenType.Tuple;
if (type.IsVendorArrayType())
return TokenType.VendorArray;
if (type.IsSubclassOf(typeof(Section)) || type == typeof(Section))
return TokenType.Section;
return TokenType.None;
}
internal static object GetObjectGenericPropertyValue(object obj, uint index)
{
Type type = obj.GetType();
if (type.IsGenericType)
{
var infos = type.GetProperties();
if (index < infos.Length)
return infos[index].GetValue(obj);
}
return null;
}
internal static T GetObjectGenericPropertyValue<T>(object obj, uint index)
{
return (T)GetObjectGenericPropertyValue(obj, index);
}
internal static object ConvertFromString(this Type type, string value)
{
try
{
if (type == typeof(Guid))
return new Guid(value.ToBytes());
if (type.IsSubclassOf(typeof(Enum)))
{
foreach (Enum e in Enum.GetValues(type))
{
var attr = e.GetAttributeOfType<UcmEnumAttribute>();
if (attr != null && attr.Name.Equals(value))
return e;
}
}
TypeConverter conv = TypeDescriptor.GetConverter(type);
return conv.ConvertFromString(value);
}
catch (NotSupportedException)
{
}
return null;
}
}
internal static class ExtensionMethods
{
internal static byte[] ToBytes(this string value)
{
var result = new List<byte>();
var substrs = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
foreach (var substr in substrs)
{
if (substr.StartsWith("0x", StringComparison.CurrentCulture) &&
byte.TryParse(substr.Substring(2), NumberStyles.HexNumber,
CultureInfo.CurrentCulture, out byte val))
result.Add(val);
else if (byte.TryParse(substr, out val))
result.Add(val);
}
return result.ToArray();
}
internal static ushort[] ToUInts16(this string value)
{
var result = new List<ushort>();
var substrs = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
foreach (var substr in substrs)
{
if (substr.StartsWith("0x", StringComparison.CurrentCulture) &&
ushort.TryParse(substr.Substring(2), NumberStyles.HexNumber,
CultureInfo.CurrentCulture, out ushort val))
result.Add(val);
else if (ushort.TryParse(substr, out val))
result.Add(val);
}
return result.ToArray();
}
internal static uint[] ToUInts32(this string value)
{
var result = new List<uint>();
var substrs = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
foreach (var substr in substrs)
{
if (substr.StartsWith("0x", StringComparison.CurrentCulture) &&
uint.TryParse(substr.Substring(2), NumberStyles.HexNumber,
CultureInfo.CurrentCulture, out uint val))
result.Add(val);
else if (uint.TryParse(substr, out val))
result.Add(val);
}
return result.ToArray();
}
internal static T GetAttributeOfType<T>(this Enum value)
where T : Attribute
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
return null;
// lets be reliable for different locales
var memInfo = type.GetMember(name);
if (memInfo.Length == 0)
return null;
return (T)Attribute.GetCustomAttribute(memInfo[0], typeof(T), false);
}
}
}