Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Scripting / Ast / TryExpression.cs / 1305376 / TryExpression.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;
#if SILVERLIGHT
using System.Core;
#endif
namespace System.Linq.Expressions {
///
/// Represents a try/catch/finally/fault block.
///
/// The body is protected by the try block.
/// The handlers consist of a set of s that can either be catch or filters.
/// The fault runs if an exception is thrown.
/// The finally runs regardless of how control exits the body.
/// Only one of fault or finally can be supplied.
/// The return type of the try block must match the return type of any associated catch statements.
///
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.TryExpressionProxy))]
#endif
public sealed class TryExpression : Expression {
private readonly Type _type;
private readonly Expression _body;
private readonly ReadOnlyCollection _handlers;
private readonly Expression _finally;
private readonly Expression _fault;
internal TryExpression(Type type, Expression body, Expression @finally, Expression fault, ReadOnlyCollection handlers) {
_type = type;
_body = body;
_handlers = handlers;
_finally = @finally;
_fault = fault;
}
///
/// Gets the static type of the expression that this represents. (Inherited from .)
///
/// The that represents the static type of the expression.
public sealed override Type Type {
get { return _type; }
}
///
/// Returns the node type of this . (Inherited from .)
///
/// The that represents this expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Try; }
}
///
/// Gets the representing the body of the try block.
///
public Expression Body {
get { return _body; }
}
///
/// Gets the collection of s associated with the try block.
///
public ReadOnlyCollection Handlers {
get { return _handlers; }
}
///
/// Gets the representing the finally block.
///
public Expression Finally {
get { return _finally; }
}
///
/// Gets the representing the fault block.
///
public Expression Fault {
get { return _fault; }
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitTry(this);
}
///
/// 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.
/// The property of the result.
/// The property of the result.
/// This expression if no children changed, or an expression with the updated children.
public TryExpression Update(Expression body, IEnumerable handlers, Expression @finally, Expression fault) {
if (body == Body && handlers == Handlers && @finally == Finally && fault == Fault) {
return this;
}
return Expression.MakeTry(Type, body, @finally, fault, handlers);
}
}
public partial class Expression {
///
/// Creates a representing a try block with a fault block and no catch statements.
///
/// The body of the try block.
/// The body of the fault block.
/// The created .
public static TryExpression TryFault(Expression body, Expression fault) {
return MakeTry(null, body, null, fault, null);
}
///
/// Creates a representing a try block with a finally block and no catch statements.
///
/// The body of the try block.
/// The body of the finally block.
/// The created .
public static TryExpression TryFinally(Expression body, Expression @finally) {
return MakeTry(null, body, @finally, null, null);
}
///
/// Creates a representing a try block with any number of catch statements and neither a fault nor finally block.
///
/// The body of the try block.
/// The array of zero or more s representing the catch statements to be associated with the try block.
/// The created .
public static TryExpression TryCatch(Expression body, params CatchBlock[] handlers) {
return MakeTry(null, body, null, null, handlers);
}
///
/// Creates a representing a try block with any number of catch statements and a finally block.
///
/// The body of the try block.
/// The body of the finally block.
/// The array of zero or more s representing the catch statements to be associated with the try block.
/// The created .
public static TryExpression TryCatchFinally(Expression body, Expression @finally, params CatchBlock[] handlers) {
return MakeTry(null, body, @finally, null, handlers);
}
///
/// Creates a representing a try block with the specified elements.
///
/// The result type of the try expression. If null, bodh and all handlers must have identical type.
/// The body of the try block.
/// The body of the finally block. Pass null if the try block has no finally block associated with it.
/// The body of the t block. Pass null if the try block has no fault block associated with it.
/// A collection of s representing the catch statements to be associated with the try block.
/// The created .
public static TryExpression MakeTry(Type type, Expression body, Expression @finally, Expression fault, IEnumerable handlers) {
RequiresCanRead(body, "body");
var @catch = handlers.ToReadOnly();
ContractUtils.RequiresNotNullItems(@catch, "handlers");
ValidateTryAndCatchHaveSameType(type, body, @catch);
if (fault != null) {
if (@finally != null || @catch.Count > 0) {
throw Error.FaultCannotHaveCatchOrFinally();
}
RequiresCanRead(fault, "fault");
} else if (@finally != null) {
RequiresCanRead(@finally, "finally");
} else if (@catch.Count == 0) {
throw Error.TryMustHaveCatchFinallyOrFault();
}
return new TryExpression(type ?? body.Type, body, @finally, fault, @catch);
}
//Validate that the body of the try expression must have the same type as the body of every try block.
private static void ValidateTryAndCatchHaveSameType(Type type, Expression tryBody, ReadOnlyCollection handlers) {
// Type unification ... all parts must be reference assignable to "type"
if (type != null) {
if (type != typeof(void)) {
if (!TypeUtils.AreReferenceAssignable(type, tryBody.Type)) {
throw Error.ArgumentTypesMustMatch();
}
foreach (var cb in handlers) {
if (!TypeUtils.AreReferenceAssignable(type, cb.Body.Type)) {
throw Error.ArgumentTypesMustMatch();
}
}
}
} else if (tryBody == null || tryBody.Type == typeof(void)) {
//The body of every try block must be null or have void type.
foreach (CatchBlock cb in handlers) {
if (cb.Body != null && cb.Body.Type != typeof(void)) {
throw Error.BodyOfCatchMustHaveSameTypeAsBodyOfTry();
}
}
} else {
//Body of every catch must have the same type of body of try.
type = tryBody.Type;
foreach (CatchBlock cb in handlers) {
if (cb.Body == null || !TypeUtils.AreEquivalent(cb.Body.Type, type)) {
throw Error.BodyOfCatchMustHaveSameTypeAsBodyOfTry();
}
}
}
}
}
}
// 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;
#if SILVERLIGHT
using System.Core;
#endif
namespace System.Linq.Expressions {
///
/// Represents a try/catch/finally/fault block.
///
/// The body is protected by the try block.
/// The handlers consist of a set of s that can either be catch or filters.
/// The fault runs if an exception is thrown.
/// The finally runs regardless of how control exits the body.
/// Only one of fault or finally can be supplied.
/// The return type of the try block must match the return type of any associated catch statements.
///
#if !SILVERLIGHT
[DebuggerTypeProxy(typeof(Expression.TryExpressionProxy))]
#endif
public sealed class TryExpression : Expression {
private readonly Type _type;
private readonly Expression _body;
private readonly ReadOnlyCollection _handlers;
private readonly Expression _finally;
private readonly Expression _fault;
internal TryExpression(Type type, Expression body, Expression @finally, Expression fault, ReadOnlyCollection handlers) {
_type = type;
_body = body;
_handlers = handlers;
_finally = @finally;
_fault = fault;
}
///
/// Gets the static type of the expression that this represents. (Inherited from .)
///
/// The that represents the static type of the expression.
public sealed override Type Type {
get { return _type; }
}
///
/// Returns the node type of this . (Inherited from .)
///
/// The that represents this expression.
public sealed override ExpressionType NodeType {
get { return ExpressionType.Try; }
}
///
/// Gets the representing the body of the try block.
///
public Expression Body {
get { return _body; }
}
///
/// Gets the collection of s associated with the try block.
///
public ReadOnlyCollection Handlers {
get { return _handlers; }
}
///
/// Gets the representing the finally block.
///
public Expression Finally {
get { return _finally; }
}
///
/// Gets the representing the fault block.
///
public Expression Fault {
get { return _fault; }
}
///
/// Dispatches to the specific visit method for this node type.
///
protected internal override Expression Accept(ExpressionVisitor visitor) {
return visitor.VisitTry(this);
}
///
/// 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.
/// The property of the result.
/// The property of the result.
/// This expression if no children changed, or an expression with the updated children.
public TryExpression Update(Expression body, IEnumerable handlers, Expression @finally, Expression fault) {
if (body == Body && handlers == Handlers && @finally == Finally && fault == Fault) {
return this;
}
return Expression.MakeTry(Type, body, @finally, fault, handlers);
}
}
public partial class Expression {
///
/// Creates a representing a try block with a fault block and no catch statements.
///
/// The body of the try block.
/// The body of the fault block.
/// The created .
public static TryExpression TryFault(Expression body, Expression fault) {
return MakeTry(null, body, null, fault, null);
}
///
/// Creates a representing a try block with a finally block and no catch statements.
///
/// The body of the try block.
/// The body of the finally block.
/// The created .
public static TryExpression TryFinally(Expression body, Expression @finally) {
return MakeTry(null, body, @finally, null, null);
}
///
/// Creates a representing a try block with any number of catch statements and neither a fault nor finally block.
///
/// The body of the try block.
/// The array of zero or more s representing the catch statements to be associated with the try block.
/// The created .
public static TryExpression TryCatch(Expression body, params CatchBlock[] handlers) {
return MakeTry(null, body, null, null, handlers);
}
///
/// Creates a representing a try block with any number of catch statements and a finally block.
///
/// The body of the try block.
/// The body of the finally block.
/// The array of zero or more s representing the catch statements to be associated with the try block.
/// The created .
public static TryExpression TryCatchFinally(Expression body, Expression @finally, params CatchBlock[] handlers) {
return MakeTry(null, body, @finally, null, handlers);
}
///
/// Creates a representing a try block with the specified elements.
///
/// The result type of the try expression. If null, bodh and all handlers must have identical type.
/// The body of the try block.
/// The body of the finally block. Pass null if the try block has no finally block associated with it.
/// The body of the t block. Pass null if the try block has no fault block associated with it.
/// A collection of s representing the catch statements to be associated with the try block.
/// The created .
public static TryExpression MakeTry(Type type, Expression body, Expression @finally, Expression fault, IEnumerable handlers) {
RequiresCanRead(body, "body");
var @catch = handlers.ToReadOnly();
ContractUtils.RequiresNotNullItems(@catch, "handlers");
ValidateTryAndCatchHaveSameType(type, body, @catch);
if (fault != null) {
if (@finally != null || @catch.Count > 0) {
throw Error.FaultCannotHaveCatchOrFinally();
}
RequiresCanRead(fault, "fault");
} else if (@finally != null) {
RequiresCanRead(@finally, "finally");
} else if (@catch.Count == 0) {
throw Error.TryMustHaveCatchFinallyOrFault();
}
return new TryExpression(type ?? body.Type, body, @finally, fault, @catch);
}
//Validate that the body of the try expression must have the same type as the body of every try block.
private static void ValidateTryAndCatchHaveSameType(Type type, Expression tryBody, ReadOnlyCollection handlers) {
// Type unification ... all parts must be reference assignable to "type"
if (type != null) {
if (type != typeof(void)) {
if (!TypeUtils.AreReferenceAssignable(type, tryBody.Type)) {
throw Error.ArgumentTypesMustMatch();
}
foreach (var cb in handlers) {
if (!TypeUtils.AreReferenceAssignable(type, cb.Body.Type)) {
throw Error.ArgumentTypesMustMatch();
}
}
}
} else if (tryBody == null || tryBody.Type == typeof(void)) {
//The body of every try block must be null or have void type.
foreach (CatchBlock cb in handlers) {
if (cb.Body != null && cb.Body.Type != typeof(void)) {
throw Error.BodyOfCatchMustHaveSameTypeAsBodyOfTry();
}
}
} else {
//Body of every catch must have the same type of body of try.
type = tryBody.Type;
foreach (CatchBlock cb in handlers) {
if (cb.Body == null || !TypeUtils.AreEquivalent(cb.Body.Type, type)) {
throw Error.BodyOfCatchMustHaveSameTypeAsBodyOfTry();
}
}
}
}
}
}
// 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
- ProcessProtocolHandler.cs
- Renderer.cs
- SqlTransaction.cs
- SvcMapFile.cs
- OdbcEnvironmentHandle.cs
- ProxyWebPartManagerDesigner.cs
- ToolStripDropTargetManager.cs
- Geometry3D.cs
- HtmlFormWrapper.cs
- NotifyParentPropertyAttribute.cs
- MsmqVerifier.cs
- KnownColorTable.cs
- NotifyCollectionChangedEventArgs.cs
- bidPrivateBase.cs
- OpCellTreeNode.cs
- DockPatternIdentifiers.cs
- JapaneseCalendar.cs
- NeutralResourcesLanguageAttribute.cs
- AsymmetricAlgorithm.cs
- OdbcTransaction.cs
- UmAlQuraCalendar.cs
- WmlPhoneCallAdapter.cs
- StringValidatorAttribute.cs
- StreamingContext.cs
- StringToken.cs
- XmlSchemaInfo.cs
- TextContainer.cs
- AssemblyFilter.cs
- PeerContact.cs
- HelpKeywordAttribute.cs
- UnsafeNativeMethods.cs
- httpserverutility.cs
- DataServiceKeyAttribute.cs
- dataprotectionpermission.cs
- TableRow.cs
- AccessDataSourceView.cs
- LogAppendAsyncResult.cs
- XmlLoader.cs
- TagPrefixCollection.cs
- BulletChrome.cs
- xmlfixedPageInfo.cs
- ShimAsPublicXamlType.cs
- XMLUtil.cs
- wgx_exports.cs
- IgnoreFileBuildProvider.cs
- CharAnimationBase.cs
- ImageCodecInfo.cs
- ObjectSet.cs
- RelationshipEndCollection.cs
- EmissiveMaterial.cs
- PeerNameResolver.cs
- DataTableReaderListener.cs
- Stackframe.cs
- ConnectionPoolManager.cs
- DataBinding.cs
- FileRegion.cs
- CodeDelegateInvokeExpression.cs
- SecureConversationServiceElement.cs
- TransportContext.cs
- XmlWrappingReader.cs
- AsyncStreamReader.cs
- ValueTable.cs
- LeafCellTreeNode.cs
- ButtonBase.cs
- AbstractDataSvcMapFileLoader.cs
- AutomationIdentifier.cs
- PassportAuthenticationModule.cs
- ExtenderProvidedPropertyAttribute.cs
- TransactionTable.cs
- HwndSourceKeyboardInputSite.cs
- milrender.cs
- DataGridViewImageCell.cs
- ConfigPathUtility.cs
- NativeMethods.cs
- StylusPointPropertyId.cs
- TextComposition.cs
- DataContract.cs
- Encoder.cs
- RuleInfoComparer.cs
- SynchronizationContext.cs
- EditorPart.cs
- newinstructionaction.cs
- XmlSerializerFactory.cs
- TypedTableBase.cs
- ServiceOperation.cs
- returneventsaver.cs
- ObjectMemberMapping.cs
- AnonymousIdentificationModule.cs
- BamlTreeNode.cs
- HttpProfileBase.cs
- HttpsHostedTransportConfiguration.cs
- ServiceDurableInstance.cs
- HttpContextBase.cs
- StateFinalizationDesigner.cs
- _OverlappedAsyncResult.cs
- Typography.cs
- WebPartConnectionsDisconnectVerb.cs
- DataObjectMethodAttribute.cs
- LoaderAllocator.cs
- CLSCompliantAttribute.cs