Beispiel #1
0
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }
            if (!(rootActivity is CompositeActivity))
            {
                throw new ArgumentException(SR.GetString(SR.Error_RootActivityTypeInvalid), "rootActivity");
            }

            CompositeActivity ownerActivity = rootActivity.TraverseDottedPathFromRoot(this.OwnerActivityDottedPath) as CompositeActivity;

            if (ownerActivity == null)
            {
                return(false);
            }

            // !!!work around:
            ownerActivity.DynamicUpdateMode = true;
            CompositeActivity addedActivityOwner = this.addedActivity.Parent;

            try
            {
                this.addedActivity.SetParent(ownerActivity);
                Activity clonedAddedActivity = this.addedActivity;
                if (!this.addedActivity.DesignMode)
                {
                    clonedAddedActivity = this.addedActivity.Clone();
                }
                // We need to serialize and deserialize in order to clone during design mode
                else
                {
                    TypeProvider     typeProvider     = WorkflowChanges.CreateTypeProvider(rootActivity);
                    ServiceContainer serviceContainer = new ServiceContainer();
                    serviceContainer.AddService(typeof(ITypeProvider), typeProvider);
                    DesignerSerializationManager manager        = new DesignerSerializationManager(serviceContainer);
                    WorkflowMarkupSerializer     xomlSerializer = new WorkflowMarkupSerializer();
                    string addedActivityText = string.Empty;
                    // serialize dynamic updates
                    using (manager.CreateSession())
                    {
                        using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
                        {
                            using (XmlWriter xmlWriter = Helpers.CreateXmlWriter(sw))
                            {
                                WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                                xomlSerializer.Serialize(xomlSerializationManager, xmlWriter, this.addedActivity);
                                addedActivityText = sw.ToString();
                            }
                        }

                        // deserialize those
                        using (StringReader sr = new StringReader(addedActivityText))
                        {
                            using (XmlReader xmlReader = XmlReader.Create(sr))
                            {
                                WorkflowMarkupSerializationManager xomlSerializationManager = new WorkflowMarkupSerializationManager(manager);
                                clonedAddedActivity = xomlSerializer.Deserialize(xomlSerializationManager, xmlReader) as Activity;
                            }
                        }
                    }
                    if (clonedAddedActivity == null)
                    {
                        throw new InvalidOperationException(SR.GetString(SR.Error_ApplyDynamicChangeFailed));
                    }
                }
                if (ownerActivity.WorkflowCoreRuntime != null)
                {
                    ((IDependencyObjectAccessor)clonedAddedActivity).InitializeInstanceForRuntime(ownerActivity.WorkflowCoreRuntime);
                }

                clonedAddedActivity.SetParent(null);
                ownerActivity.Activities.Insert(this.index, clonedAddedActivity);
            }
            finally
            {
                this.addedActivity.SetParent(addedActivityOwner);
                ownerActivity.DynamicUpdateMode = false;
            }
            return(true);
        }
        protected internal override bool ApplyTo(Activity rootActivity)
        {
            if (rootActivity == null)
            {
                throw new ArgumentNullException("rootActivity");
            }
            if (!(rootActivity is CompositeActivity))
            {
                throw new ArgumentException(SR.GetString("Error_RootActivityTypeInvalid"), "rootActivity");
            }
            CompositeActivity compositeActivity = rootActivity.TraverseDottedPathFromRoot(base.OwnerActivityDottedPath) as CompositeActivity;

            if (compositeActivity == null)
            {
                return(false);
            }
            compositeActivity.DynamicUpdateMode = true;
            CompositeActivity parent = this.addedActivity.Parent;

            try
            {
                this.addedActivity.SetParent(compositeActivity);
                Activity addedActivity = this.addedActivity;
                if (!this.addedActivity.DesignMode)
                {
                    addedActivity = this.addedActivity.Clone();
                }
                else
                {
                    TypeProvider     serviceInstance = WorkflowChanges.CreateTypeProvider(rootActivity);
                    ServiceContainer provider        = new ServiceContainer();
                    provider.AddService(typeof(ITypeProvider), serviceInstance);
                    DesignerSerializationManager manager    = new DesignerSerializationManager(provider);
                    WorkflowMarkupSerializer     serializer = new WorkflowMarkupSerializer();
                    string s = string.Empty;
                    using (manager.CreateSession())
                    {
                        using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
                        {
                            using (XmlWriter writer2 = Helpers.CreateXmlWriter(writer))
                            {
                                WorkflowMarkupSerializationManager serializationManager = new WorkflowMarkupSerializationManager(manager);
                                serializer.Serialize(serializationManager, writer2, this.addedActivity);
                                s = writer.ToString();
                            }
                        }
                        using (StringReader reader = new StringReader(s))
                        {
                            using (XmlReader reader2 = XmlReader.Create(reader))
                            {
                                WorkflowMarkupSerializationManager manager3 = new WorkflowMarkupSerializationManager(manager);
                                addedActivity = serializer.Deserialize(manager3, reader2) as Activity;
                            }
                        }
                    }
                    if (addedActivity == null)
                    {
                        throw new InvalidOperationException(SR.GetString("Error_ApplyDynamicChangeFailed"));
                    }
                }
                if (compositeActivity.WorkflowCoreRuntime != null)
                {
                    ((IDependencyObjectAccessor)addedActivity).InitializeInstanceForRuntime(compositeActivity.WorkflowCoreRuntime);
                }
                addedActivity.SetParent(null);
                compositeActivity.Activities.Insert(this.index, addedActivity);
            }
            finally
            {
                this.addedActivity.SetParent(parent);
                compositeActivity.DynamicUpdateMode = false;
            }
            return(true);
        }