blob: bbabd0dd74cdbf19fb48c6c0a81e93da2d2cd12c (
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStack
{
public static class ReflectionExtensions
{
public static bool IsInstanceOf(this Type type, Type thisOrBaseType)
{
while (type != null)
{
if (type == thisOrBaseType)
return true;
type = type.BaseType();
}
return false;
}
public static Type FirstGenericType(this Type type)
{
while (type != null)
{
if (type.IsGeneric())
return type;
type = type.BaseType();
}
return null;
}
public static Type GetTypeWithGenericTypeDefinitionOf(this Type type, Type genericTypeDefinition)
{
foreach (var t in type.GetTypeInterfaces())
{
if (t.IsGeneric() && t.GetGenericTypeDefinition() == genericTypeDefinition)
{
return t;
}
}
var genericType = type.FirstGenericType();
if (genericType != null && genericType.GetGenericTypeDefinition() == genericTypeDefinition)
{
return genericType;
}
return null;
}
public static PropertyInfo[] GetAllProperties(this Type type)
{
if (type.IsInterface())
{
var propertyInfos = new List<PropertyInfo>();
var considered = new List<Type>();
var queue = new Queue<Type>();
considered.Add(type);
queue.Enqueue(type);
while (queue.Count > 0)
{
var subType = queue.Dequeue();
foreach (var subInterface in subType.GetTypeInterfaces())
{
if (considered.Contains(subInterface)) continue;
considered.Add(subInterface);
queue.Enqueue(subInterface);
}
var typeProperties = subType.GetTypesProperties();
var newPropertyInfos = typeProperties
.Where(x => !propertyInfos.Contains(x));
propertyInfos.InsertRange(0, newPropertyInfos);
}
return propertyInfos.ToArray();
}
return type.GetTypesProperties()
.Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
.ToArray();
}
public static PropertyInfo[] GetPublicProperties(this Type type)
{
if (type.IsInterface())
{
var propertyInfos = new List<PropertyInfo>();
var considered = new List<Type>();
var queue = new Queue<Type>();
considered.Add(type);
queue.Enqueue(type);
while (queue.Count > 0)
{
var subType = queue.Dequeue();
foreach (var subInterface in subType.GetTypeInterfaces())
{
if (considered.Contains(subInterface)) continue;
considered.Add(subInterface);
queue.Enqueue(subInterface);
}
var typeProperties = subType.GetTypesPublicProperties();
var newPropertyInfos = typeProperties
.Where(x => !propertyInfos.Contains(x));
propertyInfos.InsertRange(0, newPropertyInfos);
}
return propertyInfos.ToArray();
}
return type.GetTypesPublicProperties()
.Where(t => t.GetIndexParameters().Length == 0) // ignore indexed properties
.ToArray();
}
public const string DataMember = "DataMemberAttribute";
internal static string[] IgnoreAttributesNamed = new[] {
"IgnoreDataMemberAttribute",
"JsonIgnoreAttribute"
};
public static PropertyInfo[] GetSerializableProperties(this Type type)
{
var properties = type.IsDto()
? type.GetAllProperties()
: type.GetPublicProperties();
return properties.OnlySerializableProperties(type);
}
private static List<Type> _excludeTypes = new List<Type> { typeof(Stream) };
public static PropertyInfo[] OnlySerializableProperties(this PropertyInfo[] properties, Type type = null)
{
var isDto = type.IsDto();
var readableProperties = properties.Where(x => x.PropertyGetMethod(nonPublic: isDto) != null);
if (isDto)
{
return readableProperties.Where(attr =>
attr.HasAttribute<DataMemberAttribute>()).ToArray();
}
// else return those properties that are not decorated with IgnoreDataMember
return readableProperties
.Where(prop => prop.AllAttributes()
.All(attr =>
{
var name = attr.GetType().Name;
return !IgnoreAttributesNamed.Contains(name);
}))
.Where(prop => !_excludeTypes.Contains(prop.PropertyType))
.ToArray();
}
}
public static class PlatformExtensions //Because WinRT is a POS
{
public static bool IsInterface(this Type type)
{
return type.GetTypeInfo().IsInterface;
}
public static bool IsGeneric(this Type type)
{
return type.GetTypeInfo().IsGenericType;
}
public static Type BaseType(this Type type)
{
return type.GetTypeInfo().BaseType;
}
public static Type[] GetTypeInterfaces(this Type type)
{
return type.GetTypeInfo().ImplementedInterfaces.ToArray();
}
internal static PropertyInfo[] GetTypesPublicProperties(this Type subType)
{
var pis = new List<PropertyInfo>();
foreach (var pi in subType.GetRuntimeProperties())
{
var mi = pi.GetMethod ?? pi.SetMethod;
if (mi != null && mi.IsStatic) continue;
pis.Add(pi);
}
return pis.ToArray();
}
internal static PropertyInfo[] GetTypesProperties(this Type subType)
{
var pis = new List<PropertyInfo>();
foreach (var pi in subType.GetRuntimeProperties())
{
var mi = pi.GetMethod ?? pi.SetMethod;
if (mi != null && mi.IsStatic) continue;
pis.Add(pi);
}
return pis.ToArray();
}
public static bool HasAttribute<T>(this Type type)
{
return type.AllAttributes().Any(x => x.GetType() == typeof(T));
}
public static bool HasAttribute<T>(this PropertyInfo pi)
{
return pi.AllAttributes().Any(x => x.GetType() == typeof(T));
}
public static bool IsDto(this Type type)
{
if (type == null)
return false;
return type.HasAttribute<DataContractAttribute>();
}
public static MethodInfo PropertyGetMethod(this PropertyInfo pi, bool nonPublic = false)
{
return pi.GetMethod;
}
public static object[] AllAttributes(this PropertyInfo propertyInfo)
{
return propertyInfo.GetCustomAttributes(true).ToArray();
}
public static object[] AllAttributes(this PropertyInfo propertyInfo, Type attrType)
{
return propertyInfo.GetCustomAttributes(true).Where(x => attrType.IsInstanceOf(x.GetType())).ToArray();
}
public static object[] AllAttributes(this Type type)
{
return type.GetTypeInfo().GetCustomAttributes(true).ToArray();
}
public static TAttr[] AllAttributes<TAttr>(this PropertyInfo pi)
{
return pi.AllAttributes(typeof(TAttr)).Cast<TAttr>().ToArray();
}
public static TAttr[] AllAttributes<TAttr>(this Type type)
where TAttr : Attribute
{
return type.GetTypeInfo().GetCustomAttributes<TAttr>(true).ToArray();
}
}
}
|