-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathReflectionExtensions.cs
More file actions
258 lines (235 loc) · 9.4 KB
/
Copy pathReflectionExtensions.cs
File metadata and controls
258 lines (235 loc) · 9.4 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System.Reflection;
using System.Text.RegularExpressions;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with .NET reflection objects, such as <see cref="Type" /> and <see cref="MethodBase" />.
/// </summary>
public static class ReflectionExtensions
{
private static readonly Dictionary<string, string> TypeNames = new()
{
["Void"] = "void",
["Boolean"] = "bool",
["Byte"] = "byte",
["SByte"] = "sbyte",
["Char"] = "char",
["Decimal"] = "decimal",
["Double"] = "double",
["Single"] = "float",
["Int32"] = "int",
["UInt32"] = "uint",
["Int64"] = "long",
["UInt64"] = "ulong",
["Object"] = "object",
["Int16"] = "short",
["UInt16"] = "ushort",
["IntPtr"] = "nint",
["UIntPtr"] = "nuint",
["String"] = "string"
};
extension(Type type)
{
/// <summary>
/// Gets the name of this <see cref="Type" />, including its nested class names.
/// </summary>
public string NestedName
{
get
{
Check.ArgumentNull(type);
List<string> nestedTypes = [];
Type? t = type;
do
{
nestedTypes.Add(t.Name);
t = t.IsNested ? t.DeclaringType : null;
}
while (t != null);
return nestedTypes.Reverse<string>().AsString(".");
}
}
/// <summary>
/// Gets the fully qualified name of this <see cref="Type" />, including its nested class names and its namespace but not its assembly.
/// </summary>
public string NestedFullName
{
get
{
Check.ArgumentNull(type);
return (type.Namespace == null ? null : type.Namespace + ".") + type.NestedName;
}
}
/// <summary>
/// Determines whether an instance of this <see cref="Type" /> can be assigned to the current <see cref="Type" /> instance.
/// </summary>
/// <param name="c">The <see cref="Type" /> to compare with this <see cref="Type" />.</param>
/// <param name="excludeSelf"><see langword="true" /> to exclude this <see cref="Type" /> from being checked. If this parameter is <see langword="true" /> and this <see cref="Type" /> equals <paramref name="c" />, <see langword="false" /> is returned.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="c" /> is derived either directly or indirectly derived from this <see cref="Type" />, this <see cref="Type" /> is an interface that <paramref name="c" /> implements, <paramref name="c" /> is a generic type parameter and this <see cref="Type" /> represents one of the constraints of <paramref name="c" />, <paramref name="c" /> represents a value type and this <see cref="Type" /> represents <see cref="Nullable" /><<paramref name="c" />>
/// otherwise, <see langword="false" />.
/// </returns>
public bool IsAssignableFrom(Type c, bool excludeSelf)
{
Check.ArgumentNull(type);
Check.ArgumentNull(c);
return type.IsAssignableFrom(c) && (!excludeSelf || type != c);
}
/// <summary>
/// Returns the equivalent <see cref="string" /> representation of the name of this <see cref="Type" /> using the <see cref="TypeNaming.CSharp" /> naming convention.
/// </summary>
/// <returns>
/// An equivalent <see cref="string" /> representation of the name of this <see cref="Type" /> using the <see cref="TypeNaming.CSharp" /> naming convention.
/// </returns>
public string ToCSharpName()
{
return type.ToCSharpName(TypeNaming.CSharp);
}
/// <summary>
/// Returns the equivalent <see cref="string" /> representation of the name of this <see cref="Type" /> using the specified naming convention.
/// </summary>
/// <param name="namingConvention">The <see cref="TypeNaming" /> that specifies the naming convention for the converted <see cref="Type" />.</param>
/// <returns>
/// An equivalent <see cref="string" /> representation of the name of this <see cref="Type" /> using the specified naming convention.
/// </returns>
public string ToCSharpName(TypeNaming namingConvention)
{
Check.ArgumentNull(type);
string name = namingConvention == TypeNaming.FullName ? type.NestedFullName : type.NestedName;
string suffix = "";
Type? t = type;
while (t.HasElementType)
{
Match match = Regex.Match(name, @"\[,*\]|\*|&", RegexOptions.RightToLeft);
if (!match.Success)
{
throw Throw.InvalidOperation("Could not parse element type.");
}
suffix = match.Value + suffix;
if (t.GetElementType() is Type elementType)
{
t = elementType;
}
else
{
break;
}
name = namingConvention == TypeNaming.FullName ? t.NestedFullName : t.NestedName;
}
if (namingConvention == TypeNaming.CSharp)
{
name = TypeNames.GetValueOrDefault(name, name);
}
if (t.IsGenericType)
{
string genericClass = name.SubstringUntil('`');
Type[] genericArguments = t.GetGenericArguments();
if (genericClass == nameof(Nullable<>) && genericArguments.Length == 1)
{
string nullableType = genericArguments.First().ToCSharpName(namingConvention);
return (namingConvention == TypeNaming.CSharp ? nullableType + "?" : nameof(Nullable<>) + "<" + nullableType + ">") + suffix;
}
else
{
return genericClass + "<" + genericArguments.Select(t => t.ToCSharpName(namingConvention)).AsString(", ") + ">" + suffix;
}
}
else
{
return name + suffix;
}
}
}
extension(FieldInfo field)
{
/// <summary>
/// Returns the value of a static field.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
/// <returns>
/// The value of a static field.
/// </returns>
public T? GetValue<T>()
{
return field.GetValue<T>(null);
}
/// <summary>
/// Returns the value of a field supported by a given <see cref="object" />.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
/// <param name="obj">The <see cref="object" /> to retrieve the value from.</param>
/// <returns>
/// The value of a field supported by a given <see cref="object" />.
/// </returns>
public T? GetValue<T>(object? obj)
{
Check.ArgumentNull(field);
return (T?)field.GetValue(obj);
}
}
extension(PropertyInfo property)
{
/// <summary>
/// Returns the value of a static property.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
/// <returns>
/// The value of a static property.
/// </returns>
public T? GetValue<T>()
{
return property.GetValue<T>(null);
}
/// <summary>
/// Returns the value of a property supported by a given <see cref="object" />.
/// </summary>
/// <typeparam name="T">The type to which the returned value is casted to.</typeparam>
/// <param name="obj">The <see cref="object" /> to retrieve the value from.</param>
/// <returns>
/// The value of a property supported by a given <see cref="object" />.
/// </returns>
public T? GetValue<T>(object? obj)
{
Check.ArgumentNull(property);
return (T?)property.GetValue(obj);
}
}
extension(MethodBase method)
{
/// <summary>
/// Invokes the method or constructor represented by the current instance with no parameters.
/// </summary>
/// <returns>
/// An <see cref="object" /> containing the return value of the invoked method, or null in the case of a constructor.
/// </returns>
public object? Invoke()
{
Check.ArgumentNull(method);
return method.Invoke(null, null);
}
/// <summary>
/// Invokes the method or constructor represented by the current instance with no parameters.
/// </summary>
/// <typeparam name="T">The return type of this <see cref="MethodInfo" />.</typeparam>
/// <returns>
/// An <see cref="object" /> containing the return value of the invoked method, or null in the case of a constructor.
/// </returns>
public T? Invoke<T>()
{
return method.Invoke<T>(null, null);
}
/// <summary>
/// Invokes the method or constructor represented by the current instance with, using the specified parameters.
/// </summary>
/// <typeparam name="T">The return type of this <see cref="MethodInfo" />.</typeparam>
/// <param name="obj">The <see cref="object" /> on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be <see langword="null" /> or an instance of the class that defines the constructor.</param>
/// <param name="parameters">An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, parameters should be <see langword="null" />. If the method or constructor represented by this instance takes a ref parameter, no special attribute is required for that parameter in order to invoke the method or constructor using this function. Any <see cref="object" /> in this array that is not explicitly initialized with a value will contain the default value for that <see cref="object" /> type.</param>
/// <returns>
/// An <see cref="object" /> containing the return value of the invoked method, or null in the case of a constructor.
/// </returns>
public T? Invoke<T>(object? obj, object?[]? parameters)
{
Check.ArgumentNull(method);
return (T?)method.Invoke(obj, parameters);
}
}
}