mscorlib(4.0.0.0) API with additions
EnumerableQuery.cs
1 using System.Collections;
4 using System.Reflection;
5 
6 namespace System.Linq
7 {
9  [global::__DynamicallyInvokable]
10  public abstract class EnumerableQuery
11  {
12  internal abstract Expression Expression
13  {
14  get;
15  }
16 
17  internal abstract IEnumerable Enumerable
18  {
19  get;
20  }
21 
22  internal static IQueryable Create(Type elementType, IEnumerable sequence)
23  {
24  Type type = typeof(EnumerableQuery<>).MakeGenericType(elementType);
25  return (IQueryable)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[1]
26  {
27  sequence
28  }, null);
29  }
30 
31  internal static IQueryable Create(Type elementType, Expression expression)
32  {
33  Type type = typeof(EnumerableQuery<>).MakeGenericType(elementType);
34  return (IQueryable)Activator.CreateInstance(type, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[1]
35  {
36  expression
37  }, null);
38  }
39 
41  [global::__DynamicallyInvokable]
42  protected EnumerableQuery()
43  {
44  }
45  }
48  [global::__DynamicallyInvokable]
50  {
51  private Expression expression;
52 
53  private IEnumerable<T> enumerable;
54 
57  [global::__DynamicallyInvokable]
58  IQueryProvider IQueryable.Provider
59  {
60  [global::__DynamicallyInvokable]
61  get
62  {
63  return this;
64  }
65  }
66 
67  internal override Expression Expression => expression;
68 
69  internal override IEnumerable Enumerable => enumerable;
70 
73  [global::__DynamicallyInvokable]
74  Expression IQueryable.Expression
75  {
76  [global::__DynamicallyInvokable]
77  get
78  {
79  return expression;
80  }
81  }
82 
85  [global::__DynamicallyInvokable]
86  Type IQueryable.ElementType
87  {
88  [global::__DynamicallyInvokable]
89  get
90  {
91  return typeof(T);
92  }
93  }
94 
97  [global::__DynamicallyInvokable]
98  public EnumerableQuery(IEnumerable<T> enumerable)
99  {
100  this.enumerable = enumerable;
101  expression = Expression.Constant(this);
102  }
103 
106  [global::__DynamicallyInvokable]
107  public EnumerableQuery(Expression expression)
108  {
109  this.expression = expression;
110  }
111 
115  [global::__DynamicallyInvokable]
117  {
118  if (expression == null)
119  {
120  throw Error.ArgumentNull("expression");
121  }
122  Type type = TypeHelper.FindGenericType(typeof(IQueryable<>), expression.Type);
123  if (type == null)
124  {
125  throw Error.ArgumentNotValid("expression");
126  }
127  return EnumerableQuery.Create(type.GetGenericArguments()[0], expression);
128  }
129 
134  [global::__DynamicallyInvokable]
135  IQueryable<S> IQueryProvider.CreateQuery<S>(Expression expression)
136  {
137  if (expression == null)
138  {
139  throw Error.ArgumentNull("expression");
140  }
141  if (!typeof(IQueryable<S>).IsAssignableFrom(expression.Type))
142  {
143  throw Error.ArgumentNotValid("expression");
144  }
145  return new EnumerableQuery<S>(expression);
146  }
147 
151  [global::__DynamicallyInvokable]
152  object IQueryProvider.Execute(Expression expression)
153  {
154  if (expression == null)
155  {
156  throw Error.ArgumentNull("expression");
157  }
158  Type type = typeof(EnumerableExecutor<>).MakeGenericType(expression.Type);
159  return EnumerableExecutor.Create(expression).ExecuteBoxed();
160  }
161 
166  [global::__DynamicallyInvokable]
167  S IQueryProvider.Execute<S>(Expression expression)
168  {
169  if (expression == null)
170  {
171  throw Error.ArgumentNull("expression");
172  }
173  if (!typeof(S).IsAssignableFrom(expression.Type))
174  {
175  throw Error.ArgumentNotValid("expression");
176  }
177  return new EnumerableExecutor<S>(expression).Execute();
178  }
179 
182  [global::__DynamicallyInvokable]
184  {
185  return GetEnumerator();
186  }
187 
190  [global::__DynamicallyInvokable]
192  {
193  return GetEnumerator();
194  }
195 
196  private IEnumerator<T> GetEnumerator()
197  {
198  if (enumerable == null)
199  {
200  EnumerableRewriter enumerableRewriter = new EnumerableRewriter();
201  Expression body = enumerableRewriter.Visit(this.expression);
202  Expression<Func<IEnumerable<T>>> expression = Expression.Lambda<Func<IEnumerable<T>>>(body, (IEnumerable<ParameterExpression>)null);
203  enumerable = expression.Compile()();
204  }
205  return enumerable.GetEnumerator();
206  }
207 
210  [global::__DynamicallyInvokable]
211  public override string ToString()
212  {
213  ConstantExpression constantExpression = expression as ConstantExpression;
214  if (constantExpression != null && constantExpression.Value == this)
215  {
216  if (enumerable != null)
217  {
218  return enumerable.ToString();
219  }
220  return "null";
221  }
222  return expression.ToString();
223  }
224  }
225 }
Represents an expression that has a constant value.
EnumerableQuery(IEnumerable< T > enumerable)
Initializes a new instance of the T:System.Linq.EnumerableQuery`1 class and associates it with an T:S...
Represents the result of a sorting operation.
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
object Value
Gets the value of the constant expression.
Represents an T:System.Collections.IEnumerable as an T:System.Linq.EnumerableQuery data source.
Contains methods to create types of objects locally or remotely, or obtain references to existing rem...
Definition: Activator.cs:21
virtual Type [] GetGenericArguments()
Returns an array of T:System.Type objects that represent the type arguments of a closed generic type ...
Definition: Type.cs:2433
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides the base class from which the classes that represent expression tree nodes are derived....
Definition: Expression.cs:17
override string ToString()
Returns a textual representation of the enumerable collection or, if it is null, of the expression tr...
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
IQueryable CreateQuery(Expression expression)
Constructs an T:System.Linq.IQueryable object that can evaluate the query represented by a specified ...
Provides functionality to evaluate queries against a specific data source wherein the type of the dat...
Definition: IQueryable.cs:9
virtual Type MakeGenericType(params Type[] typeArguments)
Substitutes the elements of an array of types for the type parameters of the current generic type def...
Definition: Type.cs:2398
Provides a set of static (Shared in Visual Basic) methods for querying objects that implement T:Syste...
Definition: Enumerable.cs:9
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
EnumerableQuery(Expression expression)
Initializes a new instance of the T:System.Linq.EnumerableQuery`1 class and associates the instance w...
static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture)
Creates an instance of the specified type using the constructor that best matches the specified param...
Definition: Activator.cs:57
Defines methods to create and execute queries that are described by an T:System.Linq....
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
EnumerableQuery()
Initializes a new instance of the T:System.Linq.EnumerableQuery class.