Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataWeb / Client / System / Data / Services / Client / ALinq / Evaluator.cs / 1 / Evaluator.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // //// Provides funcletization of expression tree prior to resource binding. // // // @owner [....] //--------------------------------------------------------------------- namespace System.Data.Services.Client { using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Diagnostics; #if ASTORIA_LIGHT // Hashset not available ///hack until silver light client is updated internal class HashSet: Dictionary , IEnumerable where T : class { /// hack until silver light client is updated public HashSet() { } ///hack until silver light client is updated public HashSet(IEqualityComparercomparer) : base(comparer) { } /// hack until silver light client is updated public bool Add(T value) { if (!base.ContainsKey(value)) { base.Add(value, value); return true; } return false; } ///hack until silver light client is updated public bool Contains(T value) { return base.ContainsKey(value); } ///hack until silver light client is updated new public bool Remove(T value) { return base.Remove(value); } ///hack until silver light client is updated new public IEnumeratorGetEnumerator() { return base.Keys.GetEnumerator(); } } #endif /// /// performs funcletization on an expression tree /// internal static class Evaluator { ////// Performs evaluation and replacement of independent sub-trees /// /// The root of the expression tree. /// A function that decides whether a given expression node can be part of the local function. ///A new tree with sub-trees evaluated and replaced. internal static Expression PartialEval(Expression expression, FunccanBeEvaluated) { return new SubtreeEvaluator(new Nominator(canBeEvaluated).Nominate(expression)).Eval(expression); } /// /// Performs evaluation and replacement of independent sub-trees /// /// The root of the expression tree. ///A new tree with sub-trees evaluated and replaced. internal static Expression PartialEval(Expression expression) { return PartialEval(expression, Evaluator.CanBeEvaluatedLocally); } ////// Evaluates if an expression can be evaluated locally. /// /// the expression. ///true/ false if can be evaluated locally private static bool CanBeEvaluatedLocally(Expression expression) { return expression.NodeType != ExpressionType.Parameter && expression.NodeType != ExpressionType.Lambda && expression.NodeType != (ExpressionType) ResourceExpressionType.RootResourceSet; } ////// Evaluates and replaces sub-trees when first candidate is reached (top-down) /// internal class SubtreeEvaluator : DataServiceExpressionVisitor { ///list of candidates private HashSetcandidates; /// /// constructs an expression evaluator with a list of candidates /// /// List of expressions to evaluate internal SubtreeEvaluator(HashSetcandidates) { this.candidates = candidates; } /// /// Evaluates an expression sub-tree /// /// The expression to evaluate. ///The evaluated expression. internal Expression Eval(Expression exp) { return this.Visit(exp); } ////// Visit method for visitor /// /// the expression to visit ///visited expression internal override Expression Visit(Expression exp) { if (exp == null) { return null; } if (this.candidates.Contains(exp)) { return Evaluate(exp); } return base.Visit(exp); } ////// Evaluates expression /// /// the expression to evaluate ///constant expression with return value of evaluation private static Expression Evaluate(Expression e) { if (e.NodeType == ExpressionType.Constant) { return e; } LambdaExpression lambda = Expression.Lambda(e); Delegate fn = lambda.Compile(); object value = fn.DynamicInvoke(null); Debug.Assert(!(value is Expression), "!(value is Expression)"); return Expression.Constant(value, e.Type); } } ////// Performs bottom-up analysis to determine which nodes can possibly /// be part of an evaluated sub-tree. /// internal class Nominator : DataServiceExpressionVisitor { ///func to determine whether expression can be evaluated private FuncfunctionCanBeEvaluated; /// candidate expressions for evaluation private HashSetcandidates; /// flag for when sub tree cannot be evaluated private bool cannotBeEvaluated; ////// Creates the Nominator based on the function passed. /// /// /// A Func speficying whether an expression can be evaluated or not. /// ///visited expression internal Nominator(FuncfunctionCanBeEvaluated) { this.functionCanBeEvaluated = functionCanBeEvaluated; } /// /// Nominates an expression to see if it can be evaluated /// /// /// Expression to check /// ///a list of expression sub trees that can be evaluated internal HashSetNominate(Expression expression) { this.candidates = new HashSet (EqualityComparer .Default); this.Visit(expression); return this.candidates; } /// /// Visit method for walking expression tree bottom up. /// /// /// root expression to visit /// ///visited expression internal override Expression Visit(Expression expression) { if (expression != null) { bool saveCannotBeEvaluated = this.cannotBeEvaluated; this.cannotBeEvaluated = false; base.Visit(expression); if (!this.cannotBeEvaluated) { if (this.functionCanBeEvaluated(expression)) { this.candidates.Add(expression); } else { this.cannotBeEvaluated = true; } } this.cannotBeEvaluated |= saveCannotBeEvaluated; } return expression; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // //// Provides funcletization of expression tree prior to resource binding. // // // @owner [....] //--------------------------------------------------------------------- namespace System.Data.Services.Client { using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Diagnostics; #if ASTORIA_LIGHT // Hashset not available ///hack until silver light client is updated internal class HashSet: Dictionary , IEnumerable where T : class { /// hack until silver light client is updated public HashSet() { } ///hack until silver light client is updated public HashSet(IEqualityComparercomparer) : base(comparer) { } /// hack until silver light client is updated public bool Add(T value) { if (!base.ContainsKey(value)) { base.Add(value, value); return true; } return false; } ///hack until silver light client is updated public bool Contains(T value) { return base.ContainsKey(value); } ///hack until silver light client is updated new public bool Remove(T value) { return base.Remove(value); } ///hack until silver light client is updated new public IEnumeratorGetEnumerator() { return base.Keys.GetEnumerator(); } } #endif /// /// performs funcletization on an expression tree /// internal static class Evaluator { ////// Performs evaluation and replacement of independent sub-trees /// /// The root of the expression tree. /// A function that decides whether a given expression node can be part of the local function. ///A new tree with sub-trees evaluated and replaced. internal static Expression PartialEval(Expression expression, FunccanBeEvaluated) { return new SubtreeEvaluator(new Nominator(canBeEvaluated).Nominate(expression)).Eval(expression); } /// /// Performs evaluation and replacement of independent sub-trees /// /// The root of the expression tree. ///A new tree with sub-trees evaluated and replaced. internal static Expression PartialEval(Expression expression) { return PartialEval(expression, Evaluator.CanBeEvaluatedLocally); } ////// Evaluates if an expression can be evaluated locally. /// /// the expression. ///true/ false if can be evaluated locally private static bool CanBeEvaluatedLocally(Expression expression) { return expression.NodeType != ExpressionType.Parameter && expression.NodeType != ExpressionType.Lambda && expression.NodeType != (ExpressionType) ResourceExpressionType.RootResourceSet; } ////// Evaluates and replaces sub-trees when first candidate is reached (top-down) /// internal class SubtreeEvaluator : DataServiceExpressionVisitor { ///list of candidates private HashSetcandidates; /// /// constructs an expression evaluator with a list of candidates /// /// List of expressions to evaluate internal SubtreeEvaluator(HashSetcandidates) { this.candidates = candidates; } /// /// Evaluates an expression sub-tree /// /// The expression to evaluate. ///The evaluated expression. internal Expression Eval(Expression exp) { return this.Visit(exp); } ////// Visit method for visitor /// /// the expression to visit ///visited expression internal override Expression Visit(Expression exp) { if (exp == null) { return null; } if (this.candidates.Contains(exp)) { return Evaluate(exp); } return base.Visit(exp); } ////// Evaluates expression /// /// the expression to evaluate ///constant expression with return value of evaluation private static Expression Evaluate(Expression e) { if (e.NodeType == ExpressionType.Constant) { return e; } LambdaExpression lambda = Expression.Lambda(e); Delegate fn = lambda.Compile(); object value = fn.DynamicInvoke(null); Debug.Assert(!(value is Expression), "!(value is Expression)"); return Expression.Constant(value, e.Type); } } ////// Performs bottom-up analysis to determine which nodes can possibly /// be part of an evaluated sub-tree. /// internal class Nominator : DataServiceExpressionVisitor { ///func to determine whether expression can be evaluated private FuncfunctionCanBeEvaluated; /// candidate expressions for evaluation private HashSetcandidates; /// flag for when sub tree cannot be evaluated private bool cannotBeEvaluated; ////// Creates the Nominator based on the function passed. /// /// /// A Func speficying whether an expression can be evaluated or not. /// ///visited expression internal Nominator(FuncfunctionCanBeEvaluated) { this.functionCanBeEvaluated = functionCanBeEvaluated; } /// /// Nominates an expression to see if it can be evaluated /// /// /// Expression to check /// ///a list of expression sub trees that can be evaluated internal HashSetNominate(Expression expression) { this.candidates = new HashSet (EqualityComparer .Default); this.Visit(expression); return this.candidates; } /// /// Visit method for walking expression tree bottom up. /// /// /// root expression to visit /// ///visited expression internal override Expression Visit(Expression expression) { if (expression != null) { bool saveCannotBeEvaluated = this.cannotBeEvaluated; this.cannotBeEvaluated = false; base.Visit(expression); if (!this.cannotBeEvaluated) { if (this.functionCanBeEvaluated(expression)) { this.candidates.Add(expression); } else { this.cannotBeEvaluated = true; } } this.cannotBeEvaluated |= saveCannotBeEvaluated; } return expression; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ListenerSessionConnectionReader.cs
- DbConnectionPool.cs
- HttpDebugHandler.cs
- EDesignUtil.cs
- AnnotationResourceChangedEventArgs.cs
- XmlComplianceUtil.cs
- CatalogZoneBase.cs
- login.cs
- TableLayoutPanel.cs
- SiteOfOriginContainer.cs
- IBuiltInEvidence.cs
- WebBrowser.cs
- ToolboxDataAttribute.cs
- QuaternionKeyFrameCollection.cs
- MachineKeySection.cs
- DBPropSet.cs
- DataContractSerializerSection.cs
- AssertFilter.cs
- BindToObject.cs
- HandleExceptionArgs.cs
- MappedMetaModel.cs
- BindToObject.cs
- MaskDesignerDialog.cs
- SelectionProviderWrapper.cs
- RegexReplacement.cs
- EndGetFileNameFromUserRequest.cs
- ApplicationFileCodeDomTreeGenerator.cs
- CompositeKey.cs
- ISFClipboardData.cs
- VsPropertyGrid.cs
- DetailsViewInsertEventArgs.cs
- ArgumentException.cs
- WebPartConnectionsConfigureVerb.cs
- XPathNodePointer.cs
- PerformanceCounter.cs
- BinaryObjectInfo.cs
- MetadataCollection.cs
- SoapTypeAttribute.cs
- ScriptHandlerFactory.cs
- TextTreeDeleteContentUndoUnit.cs
- Hex.cs
- ControlEvent.cs
- CodeRegionDirective.cs
- PackUriHelper.cs
- COM2ExtendedBrowsingHandler.cs
- SqlSelectStatement.cs
- DataKey.cs
- LocatorManager.cs
- ValidateNames.cs
- ArgumentNullException.cs
- PropertySegmentSerializer.cs
- MailMessage.cs
- _Semaphore.cs
- HashHelper.cs
- Point3DConverter.cs
- XmlCharCheckingReader.cs
- HttpFormatExtensions.cs
- FloaterBaseParagraph.cs
- RegionIterator.cs
- ProjectionCamera.cs
- QuaternionRotation3D.cs
- ProxyWebPartManager.cs
- EntityParameterCollection.cs
- MsmqInputMessage.cs
- HttpTransportBindingElement.cs
- WindowsHyperlink.cs
- SingleStorage.cs
- LineSegment.cs
- TextEffect.cs
- _ChunkParse.cs
- DataObjectPastingEventArgs.cs
- ListSourceHelper.cs
- RegexNode.cs
- XmlObjectSerializerWriteContext.cs
- EmptyCollection.cs
- StringComparer.cs
- XmlDomTextWriter.cs
- SystemWebExtensionsSectionGroup.cs
- PriorityBinding.cs
- Vector3DConverter.cs
- ManipulationInertiaStartingEventArgs.cs
- LockedActivityGlyph.cs
- recordstate.cs
- HtmlDocument.cs
- ClientSettings.cs
- WebPartZoneBase.cs
- CoTaskMemHandle.cs
- Point3D.cs
- shaperfactoryquerycachekey.cs
- URLString.cs
- AsymmetricKeyExchangeDeformatter.cs
- SingletonInstanceContextProvider.cs
- PowerModeChangedEventArgs.cs
- BackoffTimeoutHelper.cs
- UpdateDelegates.Generated.cs
- UnsafeNativeMethods.cs
- MinimizableAttributeTypeConverter.cs
- DebuggerAttributes.cs
- RelatedEnd.cs
- SendMailErrorEventArgs.cs