Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WF / Activities / Common / Walker.cs / 1305376 / Walker.cs
// Copyright (c) Microsoft Corporation. All rights reserved. // // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. // THE ENTIRE RISK OF USE OR RESULTS IN CONNECTION WITH THE USE OF THIS CODE // AND INFORMATION REMAINS WITH THE USER. // /********************************************************************** * NOTE: A copy of this file exists at: WF\Common\Shared * The two files must be kept in [....]. Any change made here must also * be made to WF\Common\Shared\Walker.cs *********************************************************************/ namespace System.Workflow.Activities.Common { #region Imports using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Workflow.ComponentModel; #endregion // Returns true to continue the walk, false to stop. internal delegate void WalkerEventHandler(Walker walker, WalkerEventArgs eventArgs); internal enum WalkerAction { Continue = 0, Skip = 1, Abort = 2 } #region Class WalkerEventArgs internal sealed class WalkerEventArgs: EventArgs { private Activity currentActivity = null; private object currentPropertyOwner = null; private PropertyInfo currentProperty = null; private object currentValue = null; private WalkerAction action = WalkerAction.Continue; internal WalkerEventArgs(Activity currentActivity) { this.currentActivity = currentActivity; this.currentPropertyOwner = null; this.currentProperty = null; this.currentValue = null; } internal WalkerEventArgs(Activity currentActivity, object currentValue, PropertyInfo currentProperty, object currentPropertyOwner) : this(currentActivity) { this.currentPropertyOwner = currentPropertyOwner; this.currentProperty = currentProperty; this.currentValue = currentValue; } public WalkerAction Action { get { return this.action; } set { this.action = value; } } [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public PropertyInfo CurrentProperty { get { return this.currentProperty; } } [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public object CurrentPropertyOwner { get { return this.currentPropertyOwner; } } [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public object CurrentValue { get { return this.currentValue; } } public Activity CurrentActivity { get { return this.currentActivity; } } } #endregion internal sealed class Walker { #region Members internal event WalkerEventHandler FoundActivity; internal event WalkerEventHandler FoundProperty; private bool useEnabledActivities = false; #endregion #region Methods public Walker() : this(false) { } public Walker(bool useEnabledActivities) { this.useEnabledActivities = useEnabledActivities; } public void Walk(Activity seedActivity) { Walk(seedActivity, true); } public void Walk(Activity seedActivity, bool walkChildren) { Queue queue = new Queue(); queue.Enqueue(seedActivity); while (queue.Count > 0) { Activity activity = queue.Dequeue() as Activity; if (FoundActivity != null) { WalkerEventArgs args = new WalkerEventArgs(activity); FoundActivity(this, args); if (args.Action == WalkerAction.Abort) return; if (args.Action == WalkerAction.Skip) continue; } if (FoundProperty != null) { if (!WalkProperties(activity)) return; } if (walkChildren && activity is CompositeActivity) { if (useEnabledActivities) { foreach (Activity activity2 in Helpers.GetAllEnabledActivities((CompositeActivity)activity)) queue.Enqueue(activity2); } else { foreach (Activity activity2 in ((CompositeActivity)activity).Activities) queue.Enqueue(activity2); } } } } private bool WalkProperties(Activity seedActivity) { return WalkProperties(seedActivity as Activity, seedActivity); } public bool WalkProperties(Activity activity, object obj) { Activity currentActivity = obj as Activity; PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { // !!Work around: no indexer property walking if (prop.GetIndexParameters() != null && prop.GetIndexParameters().Length > 0) continue; DesignerSerializationVisibility visibility = GetSerializationVisibility(prop); if (visibility == DesignerSerializationVisibility.Hidden) continue; //Try to see if we have dynamic property associated with the object on the same object //if so then we should compare if the dynamic property values match with the property type //if not we bail out object propValue = null; DependencyProperty dependencyProperty = DependencyProperty.FromName(prop.Name, obj.GetType()); if (dependencyProperty != null && currentActivity != null) { if (currentActivity.IsBindingSet(dependencyProperty)) propValue = currentActivity.GetBinding(dependencyProperty); else propValue = currentActivity.GetValue(dependencyProperty); } else { try { propValue = prop.CanRead ? prop.GetValue(obj, null) : null; } catch { // Eat exceptions that occur while invoking the getter. } } if (FoundProperty != null) { WalkerEventArgs args = new WalkerEventArgs(activity, propValue, prop, obj); FoundProperty(this, args); if (args.Action == WalkerAction.Skip) continue; else if (args.Action == WalkerAction.Abort) return false; } if (propValue is IList) { //We do not need to reflect on the properties of the list foreach (object childObj in (IList)propValue) { if (FoundProperty != null) { WalkerEventArgs args = new WalkerEventArgs(activity, childObj, null, propValue); FoundProperty(this, args); if (args.Action == WalkerAction.Skip) continue; else if (args.Action == WalkerAction.Abort) return false; } if(childObj != null && IsBrowsableType(childObj.GetType())) { if (!WalkProperties(activity, childObj)) return false; } } } else if (propValue != null && IsBrowsableType(propValue.GetType())) { if (!WalkProperties(activity, propValue)) return false; } } return true; } private static DesignerSerializationVisibility GetSerializationVisibility(PropertyInfo prop) { // work around!!! for Activities collection if(prop.DeclaringType == typeof(CompositeActivity) && string.Equals(prop.Name, "Activities", StringComparison.Ordinal)) return DesignerSerializationVisibility.Hidden; DesignerSerializationVisibility visibility = DesignerSerializationVisibility.Visible; DesignerSerializationVisibilityAttribute[] visibilityAttrs = (DesignerSerializationVisibilityAttribute[])prop.GetCustomAttributes(typeof(DesignerSerializationVisibilityAttribute), true); if (visibilityAttrs.Length > 0) visibility = visibilityAttrs[0].Visibility; return visibility; } private static bool IsBrowsableType(Type type) { bool browsable = false; BrowsableAttribute[] browsableAttrs = (BrowsableAttribute[])type.GetCustomAttributes(typeof(BrowsableAttribute), true); if (browsableAttrs.Length > 0) browsable = browsableAttrs[0].Browsable; return browsable; } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. // // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, // WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. // THE ENTIRE RISK OF USE OR RESULTS IN CONNECTION WITH THE USE OF THIS CODE // AND INFORMATION REMAINS WITH THE USER. // /********************************************************************** * NOTE: A copy of this file exists at: WF\Common\Shared * The two files must be kept in [....]. Any change made here must also * be made to WF\Common\Shared\Walker.cs *********************************************************************/ namespace System.Workflow.Activities.Common { #region Imports using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Workflow.ComponentModel; #endregion // Returns true to continue the walk, false to stop. internal delegate void WalkerEventHandler(Walker walker, WalkerEventArgs eventArgs); internal enum WalkerAction { Continue = 0, Skip = 1, Abort = 2 } #region Class WalkerEventArgs internal sealed class WalkerEventArgs: EventArgs { private Activity currentActivity = null; private object currentPropertyOwner = null; private PropertyInfo currentProperty = null; private object currentValue = null; private WalkerAction action = WalkerAction.Continue; internal WalkerEventArgs(Activity currentActivity) { this.currentActivity = currentActivity; this.currentPropertyOwner = null; this.currentProperty = null; this.currentValue = null; } internal WalkerEventArgs(Activity currentActivity, object currentValue, PropertyInfo currentProperty, object currentPropertyOwner) : this(currentActivity) { this.currentPropertyOwner = currentPropertyOwner; this.currentProperty = currentProperty; this.currentValue = currentValue; } public WalkerAction Action { get { return this.action; } set { this.action = value; } } [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public PropertyInfo CurrentProperty { get { return this.currentProperty; } } [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public object CurrentPropertyOwner { get { return this.currentPropertyOwner; } } [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] public object CurrentValue { get { return this.currentValue; } } public Activity CurrentActivity { get { return this.currentActivity; } } } #endregion internal sealed class Walker { #region Members internal event WalkerEventHandler FoundActivity; internal event WalkerEventHandler FoundProperty; private bool useEnabledActivities = false; #endregion #region Methods public Walker() : this(false) { } public Walker(bool useEnabledActivities) { this.useEnabledActivities = useEnabledActivities; } public void Walk(Activity seedActivity) { Walk(seedActivity, true); } public void Walk(Activity seedActivity, bool walkChildren) { Queue queue = new Queue(); queue.Enqueue(seedActivity); while (queue.Count > 0) { Activity activity = queue.Dequeue() as Activity; if (FoundActivity != null) { WalkerEventArgs args = new WalkerEventArgs(activity); FoundActivity(this, args); if (args.Action == WalkerAction.Abort) return; if (args.Action == WalkerAction.Skip) continue; } if (FoundProperty != null) { if (!WalkProperties(activity)) return; } if (walkChildren && activity is CompositeActivity) { if (useEnabledActivities) { foreach (Activity activity2 in Helpers.GetAllEnabledActivities((CompositeActivity)activity)) queue.Enqueue(activity2); } else { foreach (Activity activity2 in ((CompositeActivity)activity).Activities) queue.Enqueue(activity2); } } } } private bool WalkProperties(Activity seedActivity) { return WalkProperties(seedActivity as Activity, seedActivity); } public bool WalkProperties(Activity activity, object obj) { Activity currentActivity = obj as Activity; PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { // !!Work around: no indexer property walking if (prop.GetIndexParameters() != null && prop.GetIndexParameters().Length > 0) continue; DesignerSerializationVisibility visibility = GetSerializationVisibility(prop); if (visibility == DesignerSerializationVisibility.Hidden) continue; //Try to see if we have dynamic property associated with the object on the same object //if so then we should compare if the dynamic property values match with the property type //if not we bail out object propValue = null; DependencyProperty dependencyProperty = DependencyProperty.FromName(prop.Name, obj.GetType()); if (dependencyProperty != null && currentActivity != null) { if (currentActivity.IsBindingSet(dependencyProperty)) propValue = currentActivity.GetBinding(dependencyProperty); else propValue = currentActivity.GetValue(dependencyProperty); } else { try { propValue = prop.CanRead ? prop.GetValue(obj, null) : null; } catch { // Eat exceptions that occur while invoking the getter. } } if (FoundProperty != null) { WalkerEventArgs args = new WalkerEventArgs(activity, propValue, prop, obj); FoundProperty(this, args); if (args.Action == WalkerAction.Skip) continue; else if (args.Action == WalkerAction.Abort) return false; } if (propValue is IList) { //We do not need to reflect on the properties of the list foreach (object childObj in (IList)propValue) { if (FoundProperty != null) { WalkerEventArgs args = new WalkerEventArgs(activity, childObj, null, propValue); FoundProperty(this, args); if (args.Action == WalkerAction.Skip) continue; else if (args.Action == WalkerAction.Abort) return false; } if(childObj != null && IsBrowsableType(childObj.GetType())) { if (!WalkProperties(activity, childObj)) return false; } } } else if (propValue != null && IsBrowsableType(propValue.GetType())) { if (!WalkProperties(activity, propValue)) return false; } } return true; } private static DesignerSerializationVisibility GetSerializationVisibility(PropertyInfo prop) { // work around!!! for Activities collection if(prop.DeclaringType == typeof(CompositeActivity) && string.Equals(prop.Name, "Activities", StringComparison.Ordinal)) return DesignerSerializationVisibility.Hidden; DesignerSerializationVisibility visibility = DesignerSerializationVisibility.Visible; DesignerSerializationVisibilityAttribute[] visibilityAttrs = (DesignerSerializationVisibilityAttribute[])prop.GetCustomAttributes(typeof(DesignerSerializationVisibilityAttribute), true); if (visibilityAttrs.Length > 0) visibility = visibilityAttrs[0].Visibility; return visibility; } private static bool IsBrowsableType(Type type) { bool browsable = false; BrowsableAttribute[] browsableAttrs = (BrowsableAttribute[])type.GetCustomAttributes(typeof(BrowsableAttribute), true); if (browsableAttrs.Length > 0) browsable = browsableAttrs[0].Browsable; return browsable; } #endregion } } // 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
- EpmCustomContentDeSerializer.cs
- ScriptServiceAttribute.cs
- TriState.cs
- FixedPageStructure.cs
- ProfileParameter.cs
- HttpException.cs
- BrowserCapabilitiesCompiler.cs
- SamlAuthenticationClaimResource.cs
- FactoryGenerator.cs
- SequentialOutput.cs
- PreservationFileWriter.cs
- StructuralCache.cs
- BamlWriter.cs
- CaseCqlBlock.cs
- CompositeCollectionView.cs
- CompilerGlobalScopeAttribute.cs
- Psha1DerivedKeyGenerator.cs
- ImageFormatConverter.cs
- SettingsPropertyWrongTypeException.cs
- TextServicesProperty.cs
- DataContractJsonSerializerOperationFormatter.cs
- ErrorItem.cs
- xsdvalidator.cs
- MailWriter.cs
- DataAdapter.cs
- TabControlCancelEvent.cs
- FileDialogCustomPlacesCollection.cs
- SizeFConverter.cs
- OrCondition.cs
- DiagnosticTrace.cs
- BitmapEffectInputData.cs
- NamedElement.cs
- IdentityReference.cs
- SemaphoreSecurity.cs
- SchemaImporter.cs
- PnrpPermission.cs
- NativeCppClassAttribute.cs
- QilVisitor.cs
- XmlElementCollection.cs
- ArgumentOutOfRangeException.cs
- ScriptingProfileServiceSection.cs
- TemplateEditingFrame.cs
- TransactionState.cs
- SqlDuplicator.cs
- ImageAttributes.cs
- DataChangedEventManager.cs
- ConfigurationSchemaErrors.cs
- SkipQueryOptionExpression.cs
- RegexEditorDialog.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- XPathDescendantIterator.cs
- QuaternionRotation3D.cs
- CustomAttributeSerializer.cs
- _ListenerRequestStream.cs
- SurrogateEncoder.cs
- SoapFormatterSinks.cs
- Cursors.cs
- QueryGenerator.cs
- TiffBitmapEncoder.cs
- GetWorkflowTree.cs
- FirstMatchCodeGroup.cs
- EditBehavior.cs
- FlowDocumentReaderAutomationPeer.cs
- CacheOutputQuery.cs
- SectionRecord.cs
- AsmxEndpointPickerExtension.cs
- ToolBarButtonDesigner.cs
- ConfigurationPropertyCollection.cs
- ImageFormat.cs
- AnnotationResourceCollection.cs
- PopOutPanel.cs
- SHA1CryptoServiceProvider.cs
- MsdtcWrapper.cs
- EventlogProvider.cs
- XmlSerializerFactory.cs
- ParserContext.cs
- Filter.cs
- WebPartMovingEventArgs.cs
- DrawToolTipEventArgs.cs
- DbProviderFactoriesConfigurationHandler.cs
- ColumnResizeAdorner.cs
- DataPager.cs
- ResourceProperty.cs
- SecurityNegotiationException.cs
- sqlser.cs
- HotSpot.cs
- RefExpr.cs
- EdmToObjectNamespaceMap.cs
- IndependentlyAnimatedPropertyMetadata.cs
- PartitionResolver.cs
- _NativeSSPI.cs
- Parameter.cs
- AmbientValueAttribute.cs
- SettingsBindableAttribute.cs
- TemplateColumn.cs
- ToolStripOverflowButton.cs
- TextReader.cs
- ThreadInterruptedException.cs
- validationstate.cs
- Vector3DValueSerializer.cs