-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReflectionHelpers.cs
206 lines (181 loc) · 5.04 KB
/
ReflectionHelpers.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
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
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
#pragma warning disable IDE0044
namespace MonoSound{
internal static class ReflectionHelpers
{
/// <summary>
/// Generics handler for Marshal.SizeOf
/// </summary>
internal static class SizeOf<T>
{
static int _sizeOf;
static SizeOf()
{
_sizeOf = Marshal.SizeOf<T>();
}
static public int Get()
{
return _sizeOf;
}
}
/// <summary>
/// Fallback handler for Marshal.SizeOf(type)
/// </summary>
internal static int ManagedSizeOf(Type type)
{
return Marshal.SizeOf(type);
}
public static bool IsValueType(Type targetType)
{
if (targetType == null)
{
throw new NullReferenceException("Must supply the targetType parameter");
}
#if NET45
return targetType.GetTypeInfo().IsValueType;
#else
return targetType.IsValueType;
#endif
}
public static Type GetBaseType(Type targetType)
{
if (targetType == null)
{
throw new NullReferenceException("Must supply the targetType parameter");
}
#if NET45
return targetType.GetTypeInfo().BaseType;
#else
return targetType.BaseType;
#endif
}
/// <summary>
/// Returns the Assembly of a Type
/// </summary>
public static Assembly GetAssembly(Type targetType)
{
if (targetType == null)
{
throw new NullReferenceException("Must supply the targetType parameter");
}
#if NET45
return targetType.GetTypeInfo().Assembly;
#else
return targetType.Assembly;
#endif
}
/// <summary>
/// Returns true if the given type represents a non-object type that is not abstract.
/// </summary>
public static bool IsConcreteClass(Type t)
{
if (t == null)
{
throw new NullReferenceException("Must supply the t (type) parameter");
}
if (t == typeof(object))
return false;
#if NET45
var ti = t.GetTypeInfo();
if (ti.IsClass && !ti.IsAbstract)
return true;
#else
if (t.IsClass && !t.IsAbstract)
return true;
#endif
return false;
}
public static MethodInfo GetMethodInfo(Type type, string methodName)
{
#if NET45
return type.GetTypeInfo().GetDeclaredMethod(methodName);
#else
return type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
#endif
}
public static MethodInfo GetPropertyGetMethod(PropertyInfo property)
{
if (property == null)
{
throw new NullReferenceException("Must supply the property parameter");
}
#if NET45
return property.GetMethod;
#else
return property.GetGetMethod();
#endif
}
public static MethodInfo GetPropertySetMethod(PropertyInfo property)
{
if (property == null)
{
throw new NullReferenceException("Must supply the property parameter");
}
#if NET45
return property.SetMethod;
#else
return property.GetSetMethod();
#endif
}
public static T GetCustomAttribute<T>(MemberInfo member) where T : Attribute
{
if (member == null)
throw new NullReferenceException("Must supply the member parameter");
#if NET45
return member.GetCustomAttribute(typeof(T)) as T;
#else
return Attribute.GetCustomAttribute(member, typeof(T)) as T;
#endif
}
/// <summary>
/// Returns true if the get method of the given property exist and are public.
/// Note that we allow a getter-only property to be serialized (and deserialized),
/// *if* CanDeserializeIntoExistingObject is true for the property type.
/// </summary>
public static bool PropertyIsPublic(PropertyInfo property)
{
if (property == null)
{
throw new NullReferenceException("Must supply the property parameter");
}
var getMethod = GetPropertyGetMethod(property);
if (getMethod == null || !getMethod.IsPublic)
return false;
return true;
}
/// <summary>
/// Returns true if the given type can be assigned the given value
/// </summary>
public static bool IsAssignableFrom(Type type, object value)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (value == null)
throw new ArgumentNullException(nameof(value));
return IsAssignableFromType(type, value.GetType());
}
/// <summary>
/// Returns true if the given type can be assigned a value with the given object type
/// </summary>
public static bool IsAssignableFromType(Type type, Type objectType)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (objectType == null)
throw new ArgumentNullException(nameof(objectType));
#if NET45
if (type.GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo()))
return true;
#else
if (type.IsAssignableFrom(objectType))
return true;
#endif
return false;
}
}
}