Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / WorkflowDataContext.cs / 1305376 / WorkflowDataContext.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Activities { using System; using System.Activities.Hosting; using System.Activities.Runtime; using System.Collections.Generic; using System.ComponentModel; using System.Runtime; [Fx.Tag.XamlVisible(false)] public sealed class WorkflowDataContext : CustomTypeDescriptor, INotifyPropertyChanged, IDisposable { ActivityExecutor executor; ActivityInstance activityInstance; IDictionarylocationMapping; PropertyChangedEventHandler propertyChangedEventHandler; PropertyDescriptorCollection properties; ActivityContext cachedResolutionContext; internal WorkflowDataContext(ActivityExecutor executor, ActivityInstance activityInstance) { this.executor = executor; this.activityInstance = activityInstance; this.properties = CreateProperties(); } public event PropertyChangedEventHandler PropertyChanged; // We want our own cached ActivityContext rather than using this.executor.GetResolutionContext // because there is no synchronization of access to the executor's cached object and access thru // this WorkflowDataContext will not be done on the workflow runtime thread. ActivityContext ResolutionContext { get { if (this.cachedResolutionContext == null) { this.cachedResolutionContext = new ActivityContext(this.activityInstance, this.executor); this.cachedResolutionContext.AllowChainedEnvironmentAccess = true; } else { this.cachedResolutionContext.Reinitialize(this.activityInstance, this.executor); } return this.cachedResolutionContext; } } PropertyChangedEventHandler PropertyChangedEventHandler { get { if (this.propertyChangedEventHandler == null) { this.propertyChangedEventHandler = new PropertyChangedEventHandler(this.OnLocationChanged); } return this.propertyChangedEventHandler; } } PropertyDescriptorCollection CreateProperties() { // The name in child Activity will shadow the name in parent. Dictionary names = new Dictionary (); List propertyList = new List (); LocationReferenceEnvironment environment = this.activityInstance.Activity.PublicEnvironment; while (environment != null) { foreach (LocationReference locRef in environment.GetLocationReferences()) { AddProperty(locRef, names, propertyList); } environment = environment.Parent; } return new PropertyDescriptorCollection(propertyList.ToArray(), true); } void AddProperty(LocationReference reference, Dictionary names, List propertyList) { if (!string.IsNullOrEmpty(reference.Name) && !names.ContainsKey(reference.Name)) { names.Add(reference.Name, reference); PropertyDescriptorImpl property = new PropertyDescriptorImpl(reference); propertyList.Add(property); AddNotifyHandler(property); } } void AddNotifyHandler(PropertyDescriptorImpl property) { ActivityContext activityContext = this.ResolutionContext; try { Location location = property.LocationReference.GetLocation(activityContext); INotifyPropertyChanged notify = location as INotifyPropertyChanged; if (notify != null) { notify.PropertyChanged += PropertyChangedEventHandler; if (this.locationMapping == null) { this.locationMapping = new Dictionary (); } this.locationMapping.Add(location, property); } } finally { activityContext.Dispose(); } } void OnLocationChanged(object sender, PropertyChangedEventArgs e) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { Location location = (Location)sender; Fx.Assert(this.locationMapping != null, "Location mapping must not be null."); PropertyDescriptorImpl property; if (this.locationMapping.TryGetValue(location, out property)) { if (e.PropertyName == "Value") { handler(this, new PropertyChangedEventArgs(property.Name)); } else { handler(this, new PropertyChangedEventArgs(property.Name + "." + e.PropertyName)); } } } } public void Dispose() { if (this.locationMapping != null) { foreach (KeyValuePair pair in this.locationMapping) { INotifyPropertyChanged notify = pair.Value as INotifyPropertyChanged; if (notify != null) { notify.PropertyChanged -= PropertyChangedEventHandler; } } } } public override PropertyDescriptorCollection GetProperties() { return this.properties; } class PropertyDescriptorImpl : PropertyDescriptor { LocationReference reference; // public PropertyDescriptorImpl(LocationReference reference) : base(reference.Name, new Attribute[0]) { this.reference = reference; } public override Type ComponentType { get { return typeof(WorkflowDataContext); } } public override bool IsReadOnly { get { // return false; } } public override Type PropertyType { get { return this.reference.Type; } } public LocationReference LocationReference { get { return this.reference; } } public override bool CanResetValue(object component) { return false; } public override object GetValue(object component) { WorkflowDataContext dataContext = (WorkflowDataContext)component; ActivityContext activityContext = dataContext.ResolutionContext; try { return this.reference.GetLocation(activityContext).Value; } finally { activityContext.Dispose(); } } public override void ResetValue(object component) { throw FxTrace.Exception.AsError(new NotSupportedException(SR.CannotResetPropertyInDataContext)); } public override void SetValue(object component, object value) { if (IsReadOnly) { throw FxTrace.Exception.AsError(new NotSupportedException(SR.PropertyReadOnlyInWorkflowDataContext(this.Name))); } WorkflowDataContext dataContext = (WorkflowDataContext)component; ActivityContext activityContext = dataContext.ResolutionContext; try { Location location = this.reference.GetLocation(activityContext); location.Value = value; } finally { activityContext.Dispose(); } } public override bool ShouldSerializeValue(object component) { return true; } } } } // 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
- XmlMessageFormatter.cs
- PropertyBuilder.cs
- EntityProviderServices.cs
- ComponentRenameEvent.cs
- WarningException.cs
- DeclarationUpdate.cs
- GenericTextProperties.cs
- FlowLayoutPanel.cs
- XmlSerializerNamespaces.cs
- EdgeModeValidation.cs
- WebColorConverter.cs
- StylusShape.cs
- ListParaClient.cs
- HierarchicalDataBoundControlAdapter.cs
- HttpRequest.cs
- CompilationRelaxations.cs
- ToggleButton.cs
- Panel.cs
- DependencyPropertyAttribute.cs
- SrgsElementList.cs
- AsymmetricKeyExchangeDeformatter.cs
- CodeDomSerializer.cs
- CaseInsensitiveHashCodeProvider.cs
- RootDesignerSerializerAttribute.cs
- HttpCapabilitiesBase.cs
- ConfigXmlSignificantWhitespace.cs
- SoapMessage.cs
- SharingService.cs
- OracleBFile.cs
- Rule.cs
- ManifestResourceInfo.cs
- ButtonFlatAdapter.cs
- RequestCacheEntry.cs
- EventLogger.cs
- DetailsViewPagerRow.cs
- DbConnectionStringCommon.cs
- IdentitySection.cs
- _SpnDictionary.cs
- InstanceData.cs
- SystemResourceKey.cs
- WsiProfilesElement.cs
- BatchServiceHost.cs
- FlowLayout.cs
- Ref.cs
- WaitHandle.cs
- VirtualDirectoryMappingCollection.cs
- GeometryCollection.cs
- DBSqlParserTableCollection.cs
- PointAnimationClockResource.cs
- SafeRegistryHandle.cs
- DataGridViewControlCollection.cs
- XmlParser.cs
- AvTraceFormat.cs
- FixedSOMSemanticBox.cs
- PackageDigitalSignature.cs
- ScriptingRoleServiceSection.cs
- Command.cs
- RadioButtonList.cs
- SplitterEvent.cs
- TypeSystemProvider.cs
- UnSafeCharBuffer.cs
- TreeNodeClickEventArgs.cs
- WebPartConnectionsCancelEventArgs.cs
- FilteredReadOnlyMetadataCollection.cs
- ShapingEngine.cs
- DeclarativeExpressionConditionDeclaration.cs
- JoinTreeNode.cs
- EntityModelBuildProvider.cs
- RequestCacheManager.cs
- HtmlWindow.cs
- XPathMessageFilterElementCollection.cs
- SiteIdentityPermission.cs
- XmlAnyAttributeAttribute.cs
- HttpResponseInternalWrapper.cs
- ISAPIRuntime.cs
- GlyphElement.cs
- XmlSchemaType.cs
- ToolboxItemImageConverter.cs
- AddingNewEventArgs.cs
- precedingquery.cs
- DataSourceConverter.cs
- CombinedGeometry.cs
- InheritablePropertyChangeInfo.cs
- SecurityAlgorithmSuite.cs
- RecognizedPhrase.cs
- DictionaryTraceRecord.cs
- DelegatingTypeDescriptionProvider.cs
- ProbeMatchesMessageCD1.cs
- ScalarOps.cs
- RuleSet.cs
- Pts.cs
- MimeReturn.cs
- TemplateColumn.cs
- ToolStripManager.cs
- ComponentChangingEvent.cs
- TagMapInfo.cs
- CounterSetInstance.cs
- MimeMultiPart.cs
- DataListItemEventArgs.cs
- WebPart.cs