mscorlib(4.0.0.0) API with additions
MemberInitExpression.cs
3 using System.Diagnostics;
4 using System.Dynamic.Utils;
6 
8 {
10  [DebuggerTypeProxy(typeof(MemberInitExpressionProxy))]
11  [global::__DynamicallyInvokable]
12  public sealed class MemberInitExpression : Expression
13  {
14  private readonly NewExpression _newExpression;
15 
16  private readonly ReadOnlyCollection<MemberBinding> _bindings;
17 
20  [global::__DynamicallyInvokable]
21  public sealed override Type Type
22  {
23  [global::__DynamicallyInvokable]
24  get
25  {
26  return _newExpression.Type;
27  }
28  }
29 
32  [global::__DynamicallyInvokable]
33  public override bool CanReduce
34  {
35  [global::__DynamicallyInvokable]
36  get
37  {
38  return true;
39  }
40  }
41 
44  [global::__DynamicallyInvokable]
45  public sealed override ExpressionType NodeType
46  {
47  [global::__DynamicallyInvokable]
48  get
49  {
50  return ExpressionType.MemberInit;
51  }
52  }
53 
56  [global::__DynamicallyInvokable]
58  {
59  [global::__DynamicallyInvokable]
60  get
61  {
62  return _newExpression;
63  }
64  }
65 
68  [global::__DynamicallyInvokable]
70  {
71  [global::__DynamicallyInvokable]
72  get
73  {
74  return _bindings;
75  }
76  }
77 
79  {
80  _newExpression = newExpression;
81  _bindings = bindings;
82  }
83 
84  protected internal override Expression Accept(ExpressionVisitor visitor)
85  {
86  return visitor.VisitMemberInit(this);
87  }
88 
91  [global::__DynamicallyInvokable]
92  public override Expression Reduce()
93  {
94  return ReduceMemberInit(_newExpression, _bindings, keepOnStack: true);
95  }
96 
97  internal static Expression ReduceMemberInit(Expression objExpression, ReadOnlyCollection<MemberBinding> bindings, bool keepOnStack)
98  {
99  ParameterExpression parameterExpression = Expression.Variable(objExpression.Type, null);
100  int count = bindings.Count;
101  Expression[] array = new Expression[count + 2];
102  array[0] = Expression.Assign(parameterExpression, objExpression);
103  for (int i = 0; i < count; i++)
104  {
105  array[i + 1] = ReduceMemberBinding(parameterExpression, bindings[i]);
106  }
107  array[count + 1] = (keepOnStack ? ((Expression)parameterExpression) : ((Expression)Expression.Empty()));
108  return Expression.Block(new TrueReadOnlyCollection<Expression>(array));
109  }
110 
111  internal static Expression ReduceListInit(Expression listExpression, ReadOnlyCollection<ElementInit> initializers, bool keepOnStack)
112  {
113  ParameterExpression parameterExpression = Expression.Variable(listExpression.Type, null);
114  int count = initializers.Count;
115  Expression[] array = new Expression[count + 2];
116  array[0] = Expression.Assign(parameterExpression, listExpression);
117  for (int i = 0; i < count; i++)
118  {
119  ElementInit elementInit = initializers[i];
120  array[i + 1] = Expression.Call(parameterExpression, elementInit.AddMethod, elementInit.Arguments);
121  }
122  array[count + 1] = (keepOnStack ? ((Expression)parameterExpression) : ((Expression)Expression.Empty()));
123  return Expression.Block(new TrueReadOnlyCollection<Expression>(array));
124  }
125 
126  internal static Expression ReduceMemberBinding(ParameterExpression objVar, MemberBinding binding)
127  {
128  MemberExpression memberExpression = Expression.MakeMemberAccess(objVar, binding.Member);
129  switch (binding.BindingType)
130  {
131  case MemberBindingType.Assignment:
132  return Expression.Assign(memberExpression, ((MemberAssignment)binding).Expression);
133  case MemberBindingType.ListBinding:
134  return ReduceListInit(memberExpression, ((MemberListBinding)binding).Initializers, keepOnStack: false);
135  case MemberBindingType.MemberBinding:
136  return ReduceMemberInit(memberExpression, ((MemberMemberBinding)binding).Bindings, keepOnStack: false);
137  default:
138  throw ContractUtils.Unreachable;
139  }
140  }
141 
146  [global::__DynamicallyInvokable]
148  {
149  if (newExpression == NewExpression && bindings == Bindings)
150  {
151  return this;
152  }
153  return Expression.MemberInit(newExpression, bindings);
154  }
155  }
156 }
ReadOnlyCollection< MemberBinding > Bindings
Gets the bindings that describe how to initialize the members of the newly created object.
Provides the base class for a generic read-only collection.
Represents a constructor call.
Represents calling a constructor and initializing one or more members of the new object.
Definition: __Canon.cs:3
int Count
Gets the number of elements contained in the T:System.Collections.ObjectModel.ReadOnlyCollection`1 in...
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
Provides the base class from which the classes that represent expression tree nodes are derived....
Definition: Expression.cs:17
MemberBindingType
Describes the binding types that are used in T:System.Linq.Expressions.MemberInitExpression objects.
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
override bool CanReduce
Gets a value that indicates whether the expression tree node can be reduced.
override Expression Reduce()
Reduces the T:System.Linq.Expressions.MemberInitExpression to a simpler expression.
Represents a named parameter expression.
ExpressionType
Describes the node types for the nodes of an expression tree.
MemberInitExpression Update(NewExpression newExpression, IEnumerable< MemberBinding > bindings)
Creates a new expression that is like this one, but using the supplied children. If all of the childr...
A binding that represents recursively initializing members of a member.
sealed override ExpressionType NodeType
Returns the node type of this Expression. Extension nodes should return F:System.Linq....