/
Binder.cs
280 lines (249 loc) · 13.6 KB
/
Binder.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace Microsoft.CSharp.RuntimeBinder
{
/// <summary>
/// Contains factory methods to create dynamic call site binders for CSharp.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class Binder
{
internal const string TrimmerWarning = "Using dynamic types might cause types or members to be removed by trimmer.";
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp binary operation binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="operation">The binary operation kind.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp binary operation binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder BinaryOperation(
CSharpBinderFlags flags,
ExpressionType operation,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool isChecked = (flags & CSharpBinderFlags.CheckedContext) != 0;
bool isLogical = (flags & CSharpBinderFlags.BinaryOperationLogical) != 0;
CSharpBinaryOperationFlags binaryOperationFlags = 0;
if (isLogical)
{
binaryOperationFlags |= CSharpBinaryOperationFlags.LogicalOperation;
}
return new CSharpBinaryOperationBinder(operation, isChecked, binaryOperationFlags, context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp convert binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="type">The type to convert to.</param>
/// <param name="context">The <see cref="Type"/> that indicates where this operation is used.</param>
/// <returns>Returns a new CSharp convert binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder Convert(
CSharpBinderFlags flags,
Type type,
Type? context)
{
CSharpConversionKind conversionKind =
((flags & CSharpBinderFlags.ConvertExplicit) != 0) ?
CSharpConversionKind.ExplicitConversion :
((flags & CSharpBinderFlags.ConvertArrayIndex) != 0) ?
CSharpConversionKind.ArrayCreationConversion :
CSharpConversionKind.ImplicitConversion;
bool isChecked = (flags & CSharpBinderFlags.CheckedContext) != 0;
return new CSharpConvertBinder(type, conversionKind, isChecked, context).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp get index binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp get index binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder GetIndex(
CSharpBinderFlags flags,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
return new CSharpGetIndexBinder(context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp get member binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="name">The name of the member to get.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp get member binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder GetMember(
CSharpBinderFlags flags,
string name,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool allowCallables = (flags & CSharpBinderFlags.ResultIndexed) != 0;
return new CSharpGetMemberBinder(name, allowCallables, context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp invoke binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="context">The <see cref="Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp invoke binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder Invoke(
CSharpBinderFlags flags,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool resultDiscarded = (flags & CSharpBinderFlags.ResultDiscarded) != 0;
CSharpCallFlags callFlags = 0;
if (resultDiscarded)
{
callFlags |= CSharpCallFlags.ResultDiscarded;
}
return new CSharpInvokeBinder(callFlags, context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp invoke member binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="name">The name of the member to invoke.</param>
/// <param name="typeArguments">The list of type arguments specified for this invoke.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp invoke member binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder InvokeMember(
CSharpBinderFlags flags,
string name,
IEnumerable<Type>? typeArguments,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool invokeSimpleName = (flags & CSharpBinderFlags.InvokeSimpleName) != 0;
bool invokeSpecialName = (flags & CSharpBinderFlags.InvokeSpecialName) != 0;
bool resultDiscarded = (flags & CSharpBinderFlags.ResultDiscarded) != 0;
CSharpCallFlags callFlags = 0;
if (invokeSimpleName)
{
callFlags |= CSharpCallFlags.SimpleNameCall;
}
if (invokeSpecialName)
{
callFlags |= CSharpCallFlags.EventHookup;
}
if (resultDiscarded)
{
callFlags |= CSharpCallFlags.ResultDiscarded;
}
return new CSharpInvokeMemberBinder(callFlags, name, context, typeArguments, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp invoke constructor binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp invoke constructor binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder InvokeConstructor(
CSharpBinderFlags flags,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
return new CSharpInvokeConstructorBinder(CSharpCallFlags.None, context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp is event binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="name">The name of the event to look for.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <returns>Returns a new CSharp is event binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder IsEvent(
CSharpBinderFlags flags,
string name,
Type? context)
{
return new CSharpIsEventBinder(name, context).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp set index binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp set index binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder SetIndex(
CSharpBinderFlags flags,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool isCompoundAssignment = (flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0;
bool isChecked = (flags & CSharpBinderFlags.CheckedContext) != 0;
return new CSharpSetIndexBinder(isCompoundAssignment, isChecked, context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp set member binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="name">The name of the member to set.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp set member binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder SetMember(
CSharpBinderFlags flags,
string name,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool isCompoundAssignment = (flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0;
bool isChecked = (flags & CSharpBinderFlags.CheckedContext) != 0;
return new CSharpSetMemberBinder(name, isCompoundAssignment, isChecked, context, argumentInfo).TryGetExisting();
}
//////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new CSharp unary operation binder.
/// </summary>
/// <param name="flags">The flags with which to initialize the binder.</param>
/// <param name="operation">The unary operation kind.</param>
/// <param name="context">The <see cref="System.Type"/> that indicates where this operation is used.</param>
/// <param name="argumentInfo">The sequence of <see cref="CSharpArgumentInfo"/> instances for the arguments to this operation.</param>
/// <returns>Returns a new CSharp unary operation binder.</returns>
[RequiresUnreferencedCode(TrimmerWarning)]
public static CallSiteBinder UnaryOperation(
CSharpBinderFlags flags,
ExpressionType operation,
Type? context,
IEnumerable<CSharpArgumentInfo>? argumentInfo)
{
bool isChecked = (flags & CSharpBinderFlags.CheckedContext) != 0;
return new CSharpUnaryOperationBinder(operation, isChecked, context, argumentInfo).TryGetExisting();
}
}
}