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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Reflection;
using System.Runtime.CompilerServices;
#if SILVERLIGHT
using System.Core;
#endif
#if CLR2
namespace Microsoft.Scripting.Ast {
#else
namespace System.Linq.Expressions {
#endif
/// <summary>
/// Represents a constructor call.
/// </summary>
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.NewExpressionProxy))]
#endif
public class NewExpression : Expression, IArgumentProvider {
private readonly ConstructorInfo _constructor;
private IList<Expression> _arguments;
private readonly ReadOnlyCollection<MemberInfo> _members;
internal NewExpression(ConstructorInfo constructor, IList<Expression> arguments, ReadOnlyCollection<MemberInfo> members) {
_constructor = constructor;
_arguments = arguments;
_members = members;
}
/// <summary>
/// Gets the static type of the expression that this <see cref="Expression" /> represents. (Inherited from <see cref="Expression"/>.)
/// </summary>
/// <returns>The <see cref="Type"/> that represents the static type of the expression.</returns>
public override Type Type {
get { return _constructor.DeclaringType; }
}
/// <summary>
/// Returns the node type of this <see cref="Expression" />. (Inherited from <see cref="Expression" />.)
/// </summary>
/// <returns>The <see cref="ExpressionType"/> that represents this expression.</returns>
public sealed override ExpressionType NodeType {
get { return ExpressionType.New; }
}
/// <summary>
/// Gets the called constructor.
/// </summary>
public ConstructorInfo Constructor {
get { return _constructor; }
}
/// <summary>
/// Gets the arguments to the constructor.
/// </summary>
public ReadOnlyCollection<Expression> Arguments {
get { return ReturnReadOnly(ref _arguments); }
}
Expression IArgumentProvider.GetArgument(int index) {
return _arguments[index];
}
int IArgumentProvider.ArgumentCount {
get {
return _arguments.Count;
}
}
/// <summary>
/// Gets the members that can retrieve the values of the fields that were initialized with constructor arguments.
/// </summary>
public ReadOnlyCollection<MemberInfo> Members {
get { return _members; }
}
/// <summary>
/// Dispatches to the specific visit method for this node type.
/// </summary>
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitNew(this);
}
/// <summary>
/// Creates a new expression that is like this one, but using the
/// supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="arguments">The <see cref="Arguments" /> property of the result.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public NewExpression Update(IEnumerable<Expression> arguments) {
if (arguments == Arguments) {
return this;
}
if (Members != null) {
return Expression.New(Constructor, arguments, Members);
}
return Expression.New(Constructor, arguments);
}
}
internal class NewValueTypeExpression : NewExpression {
private readonly Type _valueType;
internal NewValueTypeExpression(Type type, ReadOnlyCollection<Expression> arguments, ReadOnlyCollection<MemberInfo> members)
: base(null, arguments, members) {
_valueType = type;
}
public sealed override Type Type {
get { return _valueType; }
}
}
public partial class Expression {
/// <summary>
/// Creates a new <see cref="NewExpression"/> that represents calling the specified constructor that takes no arguments.
/// </summary>
/// <param name="constructor">The <see cref="ConstructorInfo"/> to set the <see cref="P:Constructor"/> property equal to.</param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="P:New"/> and the <see cref="P:Constructor"/> property set to the specified value.</returns>
public static NewExpression New(ConstructorInfo constructor) {
return New(constructor, (IEnumerable<Expression>)null);
}
/// <summary>
/// Creates a new <see cref="NewExpression"/> that represents calling the specified constructor that takes no arguments.
/// </summary>
/// <param name="constructor">The <see cref="ConstructorInfo"/> to set the <see cref="P:Constructor"/> property equal to.</param>
/// <param name="arguments">An array of <see cref="Expression"/> objects to use to populate the Arguments collection.</param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="P:New"/> and the <see cref="P:Constructor"/> and <see cref="P:Arguments"/> properties set to the specified value.</returns>
public static NewExpression New(ConstructorInfo constructor, params Expression[] arguments) {
return New(constructor, (IEnumerable<Expression>)arguments);
}
/// <summary>
/// Creates a new <see cref="NewExpression"/> that represents calling the specified constructor that takes no arguments.
/// </summary>
/// <param name="constructor">The <see cref="ConstructorInfo"/> to set the <see cref="P:Constructor"/> property equal to.</param>
/// <param name="arguments">An <see cref="IEnumerable{T}"/> of <see cref="Expression"/> objects to use to populate the Arguments collection.</param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="P:New"/> and the <see cref="P:Constructor"/> and <see cref="P:Arguments"/> properties set to the specified value.</returns>
public static NewExpression New(ConstructorInfo constructor, IEnumerable<Expression> arguments) {
ContractUtils.RequiresNotNull(constructor, "constructor");
ContractUtils.RequiresNotNull(constructor.DeclaringType, "constructor.DeclaringType");
TypeUtils.ValidateType(constructor.DeclaringType);
var argList = arguments.ToReadOnly();
ValidateArgumentTypes(constructor, ExpressionType.New, ref argList);
return new NewExpression(constructor, argList, null);
}
/// <summary>
/// Creates a new <see cref="NewExpression"/> that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified.
/// </summary>
/// <param name="constructor">The <see cref="ConstructorInfo"/> to set the <see cref="P:Constructor"/> property equal to.</param>
/// <param name="arguments">An <see cref="IEnumerable{T}"/> of <see cref="Expression"/> objects to use to populate the Arguments collection.</param>
/// <param name="members">An <see cref="IEnumerable{T}"/> of <see cref="MemberInfo"/> objects to use to populate the Members collection.</param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="P:New"/> and the <see cref="P:Constructor"/>, <see cref="P:Arguments"/> and <see cref="P:Members"/> properties set to the specified value.</returns>
public static NewExpression New(ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members) {
ContractUtils.RequiresNotNull(constructor, "constructor");
var memberList = members.ToReadOnly();
var argList = arguments.ToReadOnly();
ValidateNewArgs(constructor, ref argList, ref memberList);
return new NewExpression(constructor, argList, memberList);
}
/// <summary>
/// Creates a new <see cref="NewExpression"/> that represents calling the specified constructor with the specified arguments. The members that access the constructor initialized fields are specified.
/// </summary>
/// <param name="constructor">The <see cref="ConstructorInfo"/> to set the <see cref="P:Constructor"/> property equal to.</param>
/// <param name="arguments">An <see cref="IEnumerable{T}"/> of <see cref="Expression"/> objects to use to populate the Arguments collection.</param>
/// <param name="members">An Array of <see cref="MemberInfo"/> objects to use to populate the Members collection.</param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="P:New"/> and the <see cref="P:Constructor"/>, <see cref="P:Arguments"/> and <see cref="P:Members"/> properties set to the specified value.</returns>
public static NewExpression New(ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo[] members) {
return New(constructor, arguments, (IEnumerable<MemberInfo>)members);
}
/// <summary>
/// Creates a <see cref="NewExpression"/> that represents calling the parameterless constructor of the specified type.
/// </summary>
/// <param name="type">A <see cref="Type"/> that has a constructor that takes no arguments. </param>
/// <returns>A <see cref="NewExpression"/> that has the <see cref="NodeType"/> property equal to New and the Constructor property set to the ConstructorInfo that represents the parameterless constructor of the specified type.</returns>
public static NewExpression New(Type type) {
ContractUtils.RequiresNotNull(type, "type");
if (type == typeof(void)) {
throw Error.ArgumentCannotBeOfTypeVoid();
}
ConstructorInfo ci = null;
if (!type.IsValueType) {
ci = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, System.Type.EmptyTypes, null);
if (ci == null) {
throw Error.TypeMissingDefaultConstructor(type);
}
return New(ci);
}
return new NewValueTypeExpression(type, EmptyReadOnlyCollection<Expression>.Instance, null);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
private static void ValidateNewArgs(ConstructorInfo constructor, ref ReadOnlyCollection<Expression> arguments, ref ReadOnlyCollection<MemberInfo> members) {
ParameterInfo[] pis;
if ((pis = constructor.GetParametersCached()).Length > 0) {
if (arguments.Count != pis.Length) {
throw Error.IncorrectNumberOfConstructorArguments();
}
if (arguments.Count != members.Count) {
throw Error.IncorrectNumberOfArgumentsForMembers();
}
Expression[] newArguments = null;
MemberInfo[] newMembers = null;
for (int i = 0, n = arguments.Count; i < n; i++) {
Expression arg = arguments[i];
RequiresCanRead(arg, "argument");
MemberInfo member = members[i];
ContractUtils.RequiresNotNull(member, "member");
if (!TypeUtils.AreEquivalent(member.DeclaringType, constructor.DeclaringType)) {
throw Error.ArgumentMemberNotDeclOnType(member.Name, constructor.DeclaringType.Name);
}
Type memberType;
ValidateAnonymousTypeMember(ref member, out memberType);
if (!TypeUtils.AreReferenceAssignable(memberType, arg.Type)) {
if (TypeUtils.IsSameOrSubclass(typeof(LambdaExpression), memberType) && memberType.IsAssignableFrom(arg.GetType())) {
arg = Expression.Quote(arg);
} else {
throw Error.ArgumentTypeDoesNotMatchMember(arg.Type, memberType);
}
}
ParameterInfo pi = pis[i];
Type pType = pi.ParameterType;
if (pType.IsByRef) {
pType = pType.GetElementType();
}
if (!TypeUtils.AreReferenceAssignable(pType, arg.Type)) {
if (TypeUtils.IsSameOrSubclass(typeof(LambdaExpression), pType) && pType.IsAssignableFrom(arg.Type)) {
arg = Expression.Quote(arg);
} else {
throw Error.ExpressionTypeDoesNotMatchConstructorParameter(arg.Type, pType);
}
}
if (newArguments == null && arg != arguments[i]) {
newArguments = new Expression[arguments.Count];
for (int j = 0; j < i; j++) {
newArguments[j] = arguments[j];
}
}
if (newArguments != null) {
newArguments[i] = arg;
}
if (newMembers == null && member != members[i]) {
newMembers = new MemberInfo[members.Count];
for (int j = 0; j < i; j++) {
newMembers[j] = members[j];
}
}
if (newMembers != null) {
newMembers[i] = member;
}
}
if (newArguments != null) {
arguments = new TrueReadOnlyCollection<Expression>(newArguments);
}
if (newMembers != null) {
members = new TrueReadOnlyCollection<MemberInfo>(newMembers);
}
} else if (arguments != null && arguments.Count > 0) {
throw Error.IncorrectNumberOfConstructorArguments();
} else if (members != null && members.Count > 0) {
throw Error.IncorrectNumberOfMembersForGivenConstructor();
}
}
private static void ValidateAnonymousTypeMember(ref MemberInfo member, out Type memberType) {
switch (member.MemberType) {
case MemberTypes.Field:
FieldInfo field = member as FieldInfo;
if (field.IsStatic) {
throw Error.ArgumentMustBeInstanceMember();
}
memberType = field.FieldType;
break;
case MemberTypes.Property:
PropertyInfo pi = member as PropertyInfo;
if (!pi.CanRead) {
throw Error.PropertyDoesNotHaveGetter(pi);
}
if (pi.GetGetMethod().IsStatic) {
throw Error.ArgumentMustBeInstanceMember();
}
memberType = pi.PropertyType;
break;
case MemberTypes.Method:
MethodInfo method = member as MethodInfo;
if (method.IsStatic) {
throw Error.ArgumentMustBeInstanceMember();
}
PropertyInfo prop = GetProperty(method);
member = prop;
memberType = prop.PropertyType;
break;
default:
throw Error.ArgumentMustBeFieldInfoOrPropertInfoOrMethod();
}
}
}
}
|