/
RuntimeReflectionExtensions.cs
106 lines (82 loc) · 3.75 KB
/
RuntimeReflectionExtensions.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace System.Reflection
{
public static partial class RuntimeReflectionExtensions
{
private const BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
public static IEnumerable<FieldInfo> GetRuntimeFields(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
this Type type)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetFields(Everything);
}
public static IEnumerable<MethodInfo> GetRuntimeMethods(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
this Type type)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetMethods(Everything);
}
public static IEnumerable<PropertyInfo> GetRuntimeProperties(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
this Type type)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetProperties(Everything);
}
public static IEnumerable<EventInfo> GetRuntimeEvents(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
this Type type)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetEvents(Everything);
}
public static FieldInfo? GetRuntimeField(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
this Type type, string name)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetField(name);
}
public static MethodInfo? GetRuntimeMethod(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
this Type type, string name, Type[] parameters)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetMethod(name, parameters);
}
public static PropertyInfo? GetRuntimeProperty(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
this Type type, string name)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetProperty(name);
}
public static EventInfo? GetRuntimeEvent(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
this Type type, string name)
{
ArgumentNullException.ThrowIfNull(type);
return type.GetEvent(name);
}
public static MethodInfo? GetRuntimeBaseDefinition(this MethodInfo method)
{
ArgumentNullException.ThrowIfNull(method);
return method.GetBaseDefinition();
}
public static InterfaceMapping GetRuntimeInterfaceMap(this TypeInfo typeInfo, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType)
{
ArgumentNullException.ThrowIfNull(typeInfo);
return typeInfo.GetInterfaceMap(interfaceType);
}
public static MethodInfo GetMethodInfo(this Delegate del)
{
ArgumentNullException.ThrowIfNull(del);
return del.Method;
}
}
}