-
Notifications
You must be signed in to change notification settings - Fork 1
/
Members.cs
228 lines (199 loc) · 11 KB
/
Members.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using JetBrains.Annotations;
namespace ILGeneratorExtensions
{
/// <summary>
/// Contains extension methods for manipulating type members
/// </summary>
public static partial class Members
{
/// <summary>
/// Pops a reference off the evaluation stack and calls the getter of the given property on the object
/// </summary>
/// <param name="generator"></param>
/// <param name="property">The property to get the value of</param>
[PublicAPI]
public static ILGenerator GetProperty(this ILGenerator generator, PropertyInfo property)
{
if (!property.CanRead)
{
throw new InvalidOperationException("Cannot read from this property");
}
var getMethod = property.GetGetMethod();
return generator.Call(getMethod);
}
/// <summary>
/// Pops a reference off the evaluation stack and calls the getter of the given property (looked up by name on the given type) on the object
/// </summary>
/// <remarks>Will only consider public properties, static or instance</remarks>
/// <param name="generator"></param>
/// <param name="type">The type the property belongs to</param>
/// <param name="propertyName">The name of the property on the given <paramref name="type" /></param>
[PublicAPI]
public static ILGenerator GetProperty(this ILGenerator generator, Type type, string propertyName)
=> generator.GetProperty(GetPropertyInfo(type, propertyName));
/// <summary>
/// Pops a reference off the evaluation stack and calls the getter of the given property (looked up by name on the given type) on the object
/// </summary>
/// <remarks>Will only consider public properties, static or instance</remarks>
/// <typeparam name="T">The type the property belongs to</typeparam>
/// <param name="generator"></param>
/// <param name="propertyName">The name of the property on <typeparamref name="T" /></param>
[PublicAPI]
public static ILGenerator GetProperty<T>(this ILGenerator generator, string propertyName)
=> generator.GetProperty(typeof (T), propertyName);
/// <summary>
/// Calls the getter of the static property represented by the given expression and pushes the value onto the evaluatino stack
/// </summary>
/// <typeparam name="TProp">The type of the property</typeparam>
/// <param name="generator"></param>
/// <param name="expression">An expression that accesses the relevant property</param>
[PublicAPI]
public static ILGenerator GetProperty<TProp>(this ILGenerator generator, Expression<Func<TProp>> expression)
=> generator.GetProperty(GetPropertyInfo(expression));
/// <summary>
/// Pops a reference off the evaluation stack and calls the getter of the given property on the object
/// </summary>
/// <typeparam name="T">The type the property is on</typeparam>
/// <typeparam name="TProp">The type of the property</typeparam>
/// <param name="generator"></param>
/// <param name="expression">An expression that accesses the relevant property</param>
[PublicAPI]
public static ILGenerator GetProperty<T, TProp>(this ILGenerator generator, Expression<Func<T, TProp>> expression)
=> generator.GetProperty(GetPropertyInfo(expression));
/// <summary>
/// Pops a reference and a value off the evaluation stack and calls the setter of the given property on the object with the value
/// </summary>
/// <param name="generator"></param>
/// <param name="property">The property to set</param>
[PublicAPI]
public static ILGenerator SetProperty(this ILGenerator generator, PropertyInfo property)
{
if (!property.CanWrite)
{
throw new InvalidOperationException("Cannot write to this property");
}
var setMethod = property.GetSetMethod();
return generator.Call(setMethod);
}
/// <summary>
/// Pops a reference and a value off the evaluation stack and calls the setter of the given property (looked up by name on the given type) on the object with the value
/// </summary>
/// <param name="generator"></param>
/// <param name="type">The type the property belongs to</param>
/// <param name="propertyName">The name of the property on the given <paramref name="type" /></param>
[PublicAPI]
public static ILGenerator SetProperty(this ILGenerator generator, Type type, string propertyName)
=> generator.SetProperty(GetPropertyInfo(type, propertyName));
/// <summary>
/// Pops a reference and a value off the evaluation stack and calls the setter of the given property (looked up by name on the given type) on the object with the value
/// </summary>
/// <typeparam name="T">The type the property belongs to</typeparam>
/// <param name="generator"></param>
/// <param name="propertyName">The name of the property on <typeparamref name="T" /></param>
[PublicAPI]
public static ILGenerator SetProperty<T>(this ILGenerator generator, string propertyName)
=> generator.SetProperty(typeof (T), propertyName);
/// <summary>
/// Pops a reference off the evaluation stack and calls the setter of the given property on the object
/// </summary>
/// <typeparam name="T">The type the property is on</typeparam>
/// <typeparam name="TProp">The type of the property</typeparam>
/// <param name="generator"></param>
/// <param name="expression">An expression that accesses the relevant property</param>
[PublicAPI]
public static ILGenerator SetProperty<T, TProp>(this ILGenerator generator, Expression<Func<T, TProp>> expression)
=> generator.SetProperty(GetPropertyInfo(expression));
private static PropertyInfo GetPropertyInfo(Type type, string propertyName)
{
var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
if (property == null)
{
throw new InvalidOperationException("There is no property called `" + propertyName + "` on the type " + type.Name);
}
return property;
}
private static PropertyInfo GetPropertyInfo<TProp>(Expression<Func<TProp>> expression)
{
var property = (expression.Body as MemberExpression)?.Member as PropertyInfo;
if (property == null)
{
throw new InvalidOperationException("Expression does not represent a property");
}
return property;
}
private static PropertyInfo GetPropertyInfo<T, TProp>(Expression<Func<T, TProp>> expression)
{
var property = (expression.Body as MemberExpression)?.Member as PropertyInfo;
if (property == null)
{
throw new InvalidOperationException("Expression does not represent a property");
}
return property;
}
/// <summary>
/// Pops a reference to a delegate instance off the evaluation stack and calls the adder of the given event with it
/// </summary>
/// <param name="generator"></param>
/// <param name="event">The event to add the delegate instance to</param>
[PublicAPI]
public static ILGenerator AddToEvent(this ILGenerator generator, EventInfo @event) => generator.Call(@event.AddMethod);
/// <summary>
/// Pops a reference to a delegate instance off the evaluation stack and call the adder of the event with the given name on the given type
/// </summary>
/// <remarks>Will only consider public events, static or instance</remarks>
/// <param name="generator"></param>
/// <param name="type">The type the event is on</param>
/// <param name="eventName">The name of the event</param>
[PublicAPI]
public static ILGenerator AddToEvent(this ILGenerator generator, Type type, string eventName)
=> generator.AddToEvent(GetEventInfo(type, eventName));
/// <summary>
/// Pops a reference to a delegate instance off the evaluation stack and call the adder of the event with the given name on the given type
/// </summary>
/// <remarks>Will only consider public events, static or instance</remarks>
/// <typeparam name="T">The type the event is on</typeparam>
/// <param name="generator"></param>
/// <param name="eventName">The name of the event</param>
[PublicAPI]
public static ILGenerator AddToEvent<T>(this ILGenerator generator, string eventName)
=> generator.AddToEvent(typeof (T), eventName);
/// <summary>
/// Pops a reference to a delegate instance off the evaluation stack and calls the remover of the given event with it
/// </summary>
/// <param name="generator"></param>
/// <param name="event">The event to remove the delegate instance from</param>
[PublicAPI]
public static ILGenerator RemoveFromEvent(this ILGenerator generator, EventInfo @event) => generator.Call(@event.RemoveMethod);
/// <summary>
/// Pops a reference to a delegate instance off the evaluation stack and calls the remover of the event with the given name on the given type
/// </summary>
/// <param name="generator"></param>
/// <param name="type">The type the event is on</param>
/// <param name="eventName">The name of the event</param>
[PublicAPI]
public static ILGenerator RemoveFromEvent(this ILGenerator generator, Type type, string eventName)
=> generator.RemoveFromEvent(GetEventInfo(type, eventName));
/// <summary>
/// Pops a reference to a delegate instance off the evaluation stack and calls the remover of the event with the given name on the given type
/// </summary>
/// <typeparam name="T">The type the event is on</typeparam>
/// <param name="generator"></param>
/// <param name="eventName">The name of the event</param>
[PublicAPI]
public static ILGenerator RemoveFromEvent<T>(this ILGenerator generator, string eventName)
=> generator.RemoveFromEvent(typeof (T), eventName);
private static EventInfo GetEventInfo(Type type, string eventName)
{
var eventInfo = type.GetEvent(eventName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
if (eventInfo == null)
{
throw new InvalidOperationException("There is no event called `" + eventName + "` on the type " + type.Name);
}
return eventInfo;
}
}
}