Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Scripting / Ast / InvocationExpression.cs / 1305376 / InvocationExpression.cs
/* ****************************************************************************
*
* 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Reflection;
#if SILVERLIGHT
using System.Core;
#endif
namespace System.Linq.Expressions {
///
/// Represents an expression that applies a delegate or lambda expression to a list of argument expressions.
///
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.InvocationExpressionProxy))]
#endif
public sealed class InvocationExpression : Expression, IArgumentProvider {
private IList _arguments;
private readonly Expression _lambda;
private readonly Type _returnType;
internal InvocationExpression(Expression lambda, IList arguments, Type returnType) {
_lambda = lambda;
_arguments = arguments;
_returnType = returnType;
}
///
/// Gets the static type of the expression that this represents.
///
/// The that represents the static type of the expression.
public sealed override Type Type {
get { return _returnType; }
}
///
/// Returns the node type of this Expression. Extension nodes should return
/// ExpressionType.Extension when overriding this method.
///
/// The of the expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Invoke; }
}
///
/// Gets the delegate or lambda expression to be applied.
///
public Expression Expression {
get { return _lambda; }
}
///
/// Gets the arguments that the delegate or lambda expression is applied to.
///
public ReadOnlyCollection Arguments {
get { return ReturnReadOnly(ref _arguments); }
}
///
/// 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.
///
/// The property of the result.
/// The property of the result.
/// This expression if no children changed, or an expression with the updated children.
public InvocationExpression Update(Expression expression, IEnumerable arguments) {
if (expression == Expression && arguments == Arguments) {
return this;
}
return Expression.Invoke(expression, arguments);
}
Expression IArgumentProvider.GetArgument(int index) {
return _arguments[index];
}
int IArgumentProvider.ArgumentCount {
get {
return _arguments.Count;
}
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitInvocation(this);
}
internal InvocationExpression Rewrite(Expression lambda, Expression[] arguments) {
Debug.Assert(lambda != null);
Debug.Assert(arguments == null || arguments.Length == _arguments.Count);
return Expression.Invoke(lambda, arguments ?? _arguments);
}
internal LambdaExpression LambdaOperand {
get {
return (_lambda.NodeType == ExpressionType.Quote)
? (LambdaExpression)((UnaryExpression)_lambda).Operand
: (_lambda as LambdaExpression);
}
}
}
public partial class Expression {
///
///Creates an that
///applies a delegate or lambda expression to a list of argument expressions.
///
///
///An that
///applies the specified delegate or lambda expression to the provided arguments.
///
///
///An that represents the delegate
///or lambda expression to be applied.
///
///
///An array of objects
///that represent the arguments that the delegate or lambda expression is applied to.
///
///
/// is null.
///
/// .Type does not represent a delegate type or an .-or-The property of an element of is not assignable to the type of the corresponding parameter of the delegate represented by .
///
/// does not contain the same number of elements as the list of parameters for the delegate represented by .
public static InvocationExpression Invoke(Expression expression, params Expression[] arguments) {
return Invoke(expression, (IEnumerable)arguments);
}
///
///Creates an that
///applies a delegate or lambda expression to a list of argument expressions.
///
///
///An that
///applies the specified delegate or lambda expression to the provided arguments.
///
///
///An that represents the delegate
///or lambda expression to be applied.
///
///
///An of objects
///that represent the arguments that the delegate or lambda expression is applied to.
///
///
/// is null.
///
/// .Type does not represent a delegate type or an .-or-The property of an element of is not assignable to the type of the corresponding parameter of the delegate represented by .
///
/// does not contain the same number of elements as the list of parameters for the delegate represented by .
public static InvocationExpression Invoke(Expression expression, IEnumerable arguments) {
RequiresCanRead(expression, "expression");
var args = arguments.ToReadOnly();
var mi = GetInvokeMethod(expression);
ValidateArgumentTypes(mi, ExpressionType.Invoke, ref args);
return new InvocationExpression(expression, args, mi.ReturnType);
}
///
/// Gets the delegate's Invoke method; used by InvocationExpression.
///
/// The expression to be invoked.
internal static MethodInfo GetInvokeMethod(Expression expression) {
Type delegateType = expression.Type;
if (delegateType == typeof(Delegate)) {
throw Error.ExpressionTypeNotInvocable(delegateType);
} else if (!typeof(Delegate).IsAssignableFrom(expression.Type)) {
Type exprType = TypeUtils.FindGenericType(typeof(Expression<>), expression.Type);
if (exprType == null) {
throw Error.ExpressionTypeNotInvocable(expression.Type);
}
delegateType = exprType.GetGenericArguments()[0];
}
return delegateType.GetMethod("Invoke");
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
/* ****************************************************************************
*
* 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.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Dynamic.Utils;
using System.Reflection;
#if SILVERLIGHT
using System.Core;
#endif
namespace System.Linq.Expressions {
///
/// Represents an expression that applies a delegate or lambda expression to a list of argument expressions.
///
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.InvocationExpressionProxy))]
#endif
public sealed class InvocationExpression : Expression, IArgumentProvider {
private IList _arguments;
private readonly Expression _lambda;
private readonly Type _returnType;
internal InvocationExpression(Expression lambda, IList arguments, Type returnType) {
_lambda = lambda;
_arguments = arguments;
_returnType = returnType;
}
///
/// Gets the static type of the expression that this represents.
///
/// The that represents the static type of the expression.
public sealed override Type Type {
get { return _returnType; }
}
///
/// Returns the node type of this Expression. Extension nodes should return
/// ExpressionType.Extension when overriding this method.
///
/// The of the expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Invoke; }
}
///
/// Gets the delegate or lambda expression to be applied.
///
public Expression Expression {
get { return _lambda; }
}
///
/// Gets the arguments that the delegate or lambda expression is applied to.
///
public ReadOnlyCollection Arguments {
get { return ReturnReadOnly(ref _arguments); }
}
///
/// 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.
///
/// The property of the result.
/// The property of the result.
/// This expression if no children changed, or an expression with the updated children.
public InvocationExpression Update(Expression expression, IEnumerable arguments) {
if (expression == Expression && arguments == Arguments) {
return this;
}
return Expression.Invoke(expression, arguments);
}
Expression IArgumentProvider.GetArgument(int index) {
return _arguments[index];
}
int IArgumentProvider.ArgumentCount {
get {
return _arguments.Count;
}
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitInvocation(this);
}
internal InvocationExpression Rewrite(Expression lambda, Expression[] arguments) {
Debug.Assert(lambda != null);
Debug.Assert(arguments == null || arguments.Length == _arguments.Count);
return Expression.Invoke(lambda, arguments ?? _arguments);
}
internal LambdaExpression LambdaOperand {
get {
return (_lambda.NodeType == ExpressionType.Quote)
? (LambdaExpression)((UnaryExpression)_lambda).Operand
: (_lambda as LambdaExpression);
}
}
}
public partial class Expression {
///
///Creates an that
///applies a delegate or lambda expression to a list of argument expressions.
///
///
///An that
///applies the specified delegate or lambda expression to the provided arguments.
///
///
///An that represents the delegate
///or lambda expression to be applied.
///
///
///An array of objects
///that represent the arguments that the delegate or lambda expression is applied to.
///
///
/// is null.
///
/// .Type does not represent a delegate type or an .-or-The property of an element of is not assignable to the type of the corresponding parameter of the delegate represented by .
///
/// does not contain the same number of elements as the list of parameters for the delegate represented by .
public static InvocationExpression Invoke(Expression expression, params Expression[] arguments) {
return Invoke(expression, (IEnumerable)arguments);
}
///
///Creates an that
///applies a delegate or lambda expression to a list of argument expressions.
///
///
///An that
///applies the specified delegate or lambda expression to the provided arguments.
///
///
///An that represents the delegate
///or lambda expression to be applied.
///
///
///An of objects
///that represent the arguments that the delegate or lambda expression is applied to.
///
///
/// is null.
///
/// .Type does not represent a delegate type or an .-or-The property of an element of is not assignable to the type of the corresponding parameter of the delegate represented by .
///
/// does not contain the same number of elements as the list of parameters for the delegate represented by .
public static InvocationExpression Invoke(Expression expression, IEnumerable arguments) {
RequiresCanRead(expression, "expression");
var args = arguments.ToReadOnly();
var mi = GetInvokeMethod(expression);
ValidateArgumentTypes(mi, ExpressionType.Invoke, ref args);
return new InvocationExpression(expression, args, mi.ReturnType);
}
///
/// Gets the delegate's Invoke method; used by InvocationExpression.
///
/// The expression to be invoked.
internal static MethodInfo GetInvokeMethod(Expression expression) {
Type delegateType = expression.Type;
if (delegateType == typeof(Delegate)) {
throw Error.ExpressionTypeNotInvocable(delegateType);
} else if (!typeof(Delegate).IsAssignableFrom(expression.Type)) {
Type exprType = TypeUtils.FindGenericType(typeof(Expression<>), expression.Type);
if (exprType == null) {
throw Error.ExpressionTypeNotInvocable(expression.Type);
}
delegateType = exprType.GetGenericArguments()[0];
}
return delegateType.GetMethod("Invoke");
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ToolStripDropDownClosingEventArgs.cs
- CachedCompositeFamily.cs
- RouteData.cs
- DockPanel.cs
- EntityUtil.cs
- SafeEventLogReadHandle.cs
- OleDbError.cs
- XmlEncoding.cs
- BeginEvent.cs
- TripleDESCryptoServiceProvider.cs
- OrthographicCamera.cs
- ObjectDataSourceView.cs
- ModuleElement.cs
- BamlLocalizabilityResolver.cs
- ParameterCollection.cs
- MinimizableAttributeTypeConverter.cs
- FixedSOMContainer.cs
- BuildProviderAppliesToAttribute.cs
- _HeaderInfo.cs
- ClientType.cs
- InkPresenter.cs
- CultureInfoConverter.cs
- ShowExpandedMultiValueConverter.cs
- EpmTargetPathSegment.cs
- SpotLight.cs
- DataKeyPropertyAttribute.cs
- XamlTreeBuilder.cs
- CodeAttributeArgumentCollection.cs
- NavigationFailedEventArgs.cs
- QuadraticBezierSegment.cs
- Query.cs
- CommandValueSerializer.cs
- NavigationHelper.cs
- HttpServerVarsCollection.cs
- Journal.cs
- KeyFrames.cs
- FontStyleConverter.cs
- DataGridViewImageColumn.cs
- QuadraticBezierSegment.cs
- DataGridViewElement.cs
- TargetInvocationException.cs
- QilList.cs
- Pen.cs
- WebSysDescriptionAttribute.cs
- SiteMapPath.cs
- XamlWriter.cs
- RSACryptoServiceProvider.cs
- WmfPlaceableFileHeader.cs
- MemoryStream.cs
- TempEnvironment.cs
- GridViewRowEventArgs.cs
- LongValidatorAttribute.cs
- ElementNotAvailableException.cs
- InstanceHandleConflictException.cs
- ErasingStroke.cs
- FreezableOperations.cs
- NamespaceInfo.cs
- SetState.cs
- ListItemConverter.cs
- BooleanAnimationUsingKeyFrames.cs
- GregorianCalendar.cs
- QilScopedVisitor.cs
- RegexCaptureCollection.cs
- HttpModuleCollection.cs
- Fonts.cs
- ListControlConvertEventArgs.cs
- AppearanceEditorPart.cs
- XmlSchemaObjectCollection.cs
- TreeNodeEventArgs.cs
- NameValuePair.cs
- EntityDataReader.cs
- ProtectedProviderSettings.cs
- Byte.cs
- SafeIUnknown.cs
- SizeF.cs
- HandlerBase.cs
- entityreference_tresulttype.cs
- ResourceProperty.cs
- InstalledFontCollection.cs
- CustomCredentialPolicy.cs
- ColumnBinding.cs
- EditingCommands.cs
- ListenerChannelContext.cs
- WebPartTracker.cs
- ClickablePoint.cs
- CacheEntry.cs
- Cloud.cs
- RightsManagementProvider.cs
- IisTraceListener.cs
- embossbitmapeffect.cs
- ObjectStateFormatter.cs
- XmlArrayAttribute.cs
- StylusLogic.cs
- CalloutQueueItem.cs
- EditorPart.cs
- DocumentReference.cs
- PrintDialog.cs
- DLinqColumnProvider.cs
- AttributeXamlType.cs
- Effect.cs