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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace ICSharpCode.NRefactory6.CSharp
{
[Flags]
internal enum BindingFlags
{
Default = 0,
Instance = 1,
Static = 2,
Public = 4,
NonPublic = 8,
}
internal static class ReflectionCompatibilityExtensions
{
public static object[] GetCustomAttributes(this Type type, bool inherit)
{
return type.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
}
public static MethodInfo GetMethod(this Type type, string name)
{
return type.GetTypeInfo().DeclaredMethods.FirstOrDefault(m => m.Name == name);
}
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags)
{
return type.GetTypeInfo().DeclaredMethods.FirstOrDefault(m => (m.Name == name) && IsConformWithBindingFlags(m, bindingFlags));
}
public static MethodInfo GetMethod(this Type type, string name, Type[] types)
{
return type.GetTypeInfo().DeclaredMethods.FirstOrDefault(
m => (m.Name == name) && TypesAreEqual(m.GetParameters().Select(p => p.ParameterType).ToArray(), types));
}
public static MethodInfo GetMethod(this Type type, string name, BindingFlags bindingFlags, object binder, Type[] types, object modifiers)
{
return type.GetTypeInfo().DeclaredMethods.FirstOrDefault(
m => (m.Name == name) && IsConformWithBindingFlags(m, bindingFlags) && TypesAreEqual(m.GetParameters().Select(p => p.ParameterType).ToArray(), types));
}
public static IEnumerable<MethodInfo> GetMethods(this Type type)
{
return type.GetTypeInfo().DeclaredMethods;
}
public static IEnumerable<MethodInfo> GetMethods(this Type type, string name)
{
return type.GetTypeInfo().DeclaredMethods.Where(m => m.Name == name);
}
public static IEnumerable<MethodInfo> GetMethods(this Type type, BindingFlags bindingFlags)
{
return type.GetTypeInfo().DeclaredMethods.Where(m => IsConformWithBindingFlags(m, bindingFlags));
}
public static FieldInfo GetField(this Type type, string name)
{
return type.GetTypeInfo().DeclaredFields.FirstOrDefault(f => f.Name == name);
}
public static FieldInfo GetField(this Type type, string name, BindingFlags bindingFlags)
{
return type.GetTypeInfo().DeclaredFields.FirstOrDefault(f => (f.Name == name) && IsConformWithBindingFlags(f, bindingFlags));
}
public static PropertyInfo GetProperty(this Type type, string name)
{
return type.GetTypeInfo().DeclaredProperties.FirstOrDefault(p => p.Name == name);
}
public static IEnumerable<PropertyInfo> GetProperties(this Type type)
{
return type.GetTypeInfo().DeclaredProperties;
}
private static bool TypesAreEqual(Type[] memberTypes, Type[] searchedTypes)
{
if (((memberTypes == null) || (searchedTypes == null)) && (memberTypes != searchedTypes))
return false;
if (memberTypes.Length != searchedTypes.Length)
return false;
for (int i = 0; i < memberTypes.Length; i++)
{
if (memberTypes[i] != searchedTypes[i])
return false;
}
return true;
}
private static bool IsConformWithBindingFlags(MethodBase method, BindingFlags bindingFlags)
{
if (method.IsPublic && !bindingFlags.HasFlag(BindingFlags.Public))
return false;
if (method.IsPrivate && !bindingFlags.HasFlag(BindingFlags.NonPublic))
return false;
if (method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Static))
return false;
if (!method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Instance))
return false;
return true;
}
private static bool IsConformWithBindingFlags(FieldInfo method, BindingFlags bindingFlags)
{
if (method.IsPublic && !bindingFlags.HasFlag(BindingFlags.Public))
return false;
if (method.IsPrivate && !bindingFlags.HasFlag(BindingFlags.NonPublic))
return false;
if (method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Static))
return false;
if (!method.IsStatic && !bindingFlags.HasFlag(BindingFlags.Instance))
return false;
return true;
}
}
}
|