Ejemplo n.º 1
0
        private static void ConvertNamespaces(object root, EditingContext context)
        {
            VisualBasicSettings       settings = VisualBasic.GetSettings(root);
            IList <AssemblyReference> references;
            IList <string>            importedNamespaces = NamespaceHelper.GetTextExpressionNamespaces(root, out references);
            FrameworkName             targetFramework    = WorkflowDesigner.GetTargetFramework(context);

            if (targetFramework.IsLessThan45())
            {
                if (settings == null)
                {
                    if ((importedNamespaces != null) && (importedNamespaces.Count > 0))
                    {
                        NamespaceHelper.ConvertToVBSettings(
                            importedNamespaces,
                            references,
                            context,
                            out settings);
                    }
                    else
                    {
                        settings = new VisualBasicSettings();
                    }

                    NamespaceHelper.SetVisualBasicSettings(root, settings);
                    NamespaceHelper.SetTextExpressionNamespaces(root, null, null);
                }

                IDebuggableWorkflowTree debuggableWorkflowTree = root as IDebuggableWorkflowTree;
                if (debuggableWorkflowTree != null)
                {
                    Activity rootActivity = debuggableWorkflowTree.GetWorkflowRoot();
                    if (rootActivity != null)
                    {
                        NamespaceHelper.SetVisualBasicSettings(rootActivity, settings);
                        NamespaceHelper.SetTextExpressionNamespaces(rootActivity, null, null);
                    }
                }
            }
            else
            {
                if ((importedNamespaces == null) || (importedNamespaces.Count == 0))
                {
                    if (settings != null)
                    {
                        NamespaceHelper.ConvertToTextExpressionImports(settings, out importedNamespaces, out references);
                        NamespaceHelper.SetTextExpressionNamespaces(root, importedNamespaces, references);
                        NamespaceHelper.SetVisualBasicSettings(root, null);
                    }
                    else
                    {
                        NamespaceHelper.SetTextExpressionNamespaces(root, new Collection <string>(), new Collection <AssemblyReference>());
                    }
                }
            }
        }
        internal string SerializeToString(object obj, string fileName)
        {
            FrameworkName targetFramework = this.FrameworkName;

            string   sourceFile          = null;
            Activity rootWorkflowElement = GetRootWorkflowElement(obj);
            Dictionary <int, object> modelItemObjectSequence = null;

            // If the current target is 4.5 or Higher, let us not write the filename attribute as DebugSymbol eliminates the need for it.
            // We will serialize without the Filename by removing it from the element and adding it back after serialization
            if (targetFramework.Is45OrHigher())
            {
                if (AttachablePropertyServices.TryGetProperty <string>(rootWorkflowElement, XamlDebuggerXmlReader.FileNameName, out sourceFile))
                {
                    AttachablePropertyServices.RemoveProperty(rootWorkflowElement, XamlDebuggerXmlReader.FileNameName);
                }
            }

            TextWriter textWriter = new StringWriter(CultureInfo.InvariantCulture);

            WorkflowDesignerXamlSchemaContext schemaContext = obj is ActivityBuilder ? this.XamlSchemaContext : new WorkflowDesignerXamlSchemaContext(null);

            bool shouldWriteDebugSymbol = true;

            if (targetFramework.IsLessThan45())
            {
                shouldWriteDebugSymbol = false;
            }

            System.Xaml.XamlReader       outerReader;
            XamlObjectReaderWithSequence innerReader;

            using (textWriter)
            {
                UsingXamlWriter(
                    new DesignTimeXamlWriter(textWriter, schemaContext, shouldWriteDebugSymbol),
                    delegate(DesignTimeXamlWriter designTimeXamlWriter)
                {
                    UsingXamlWriter(
                        ActivityXamlServices.CreateBuilderWriter(designTimeXamlWriter),
                        delegate(System.Xaml.XamlWriter activityBuilderWriter)
                    {
                        UsingXamlWriter(
                            new ActivityTemplateFactoryBuilderWriter(activityBuilderWriter, schemaContext),
                            delegate(System.Xaml.XamlWriter writer)
                        {
                            // If ViewStateManager property is attached, remove it. It needs to be regenerated if we target 4.5.
                            // It should be removed if we're targeting 4.0.
                            AttachablePropertyServices.RemoveProperty(obj, WorkflowViewState.ViewStateManagerProperty);

                            this.CreateXamlObjectReaders(obj, schemaContext, out outerReader, out innerReader);

                            using (innerReader)
                            {
                                using (outerReader)
                                {
#if ERROR_TOLERANT_SUPPORT
                                    if (ErrorActivity.GetHasErrorActivities(obj))
                                    {
                                        ErrorTolerantObjectWriter.TransformAndStripErrors(outerReader, writer);
                                    }
                                    else
                                    {
#endif
                                    XamlServices.Transform(outerReader, writer);
#if ERROR_TOLERANT_SUPPORT
                                }
#endif
                                }
                            }

                            modelItemObjectSequence = innerReader.SequenceNumberToObjectMap;
                        });
                    });
                });
            }

            string retVal = textWriter.ToString();

            if (targetFramework.IsLessThan45())
            {
                if (sourceFile != null)
                {
                    XamlDebuggerXmlReader.SetFileName(rootWorkflowElement, sourceFile);
                }
            }

            IList <XamlLoadErrorInfo> loadErrors;
            Dictionary <object, SourceLocation> sourceLocations;
            object deserializedObject = this.DeserializeString(retVal, out loadErrors, out sourceLocations);

            if (!string.IsNullOrEmpty(fileName) && targetFramework.Is45OrHigher())
            {
                this.LastWorkflowSymbol = this.GetWorkflowSymbol(fileName, deserializedObject, sourceLocations);
                if (this.LastWorkflowSymbol != null)
                {
                    retVal = retVal.Replace(DesignTimeXamlWriter.EmptyWorkflowSymbol, this.LastWorkflowSymbol.Encode());
                }
            }

            // The symbol is actually removed in GetAttachedWorkflowSymbol() after deserialization completes.
            System.Xaml.AttachablePropertyServices.RemoveProperty(GetRootWorkflowElement(deserializedObject), DebugSymbol.SymbolName);
            this.CreateXamlObjectReaders(deserializedObject, schemaContext, out outerReader, out innerReader);
            Dictionary <object, object> sourceLocationObjectToModelItemObjectMapping = new Dictionary <object, object>(ObjectReferenceEqualityComparer <object> .Default);
            using (innerReader)
            {
                using (outerReader)
                {
                    while (outerReader.Read())
                    {
                    }

                    Dictionary <int, object> sourceLocationObjectSequence = innerReader.SequenceNumberToObjectMap;
                    foreach (KeyValuePair <int, object> sourceLocationObjectEntry in sourceLocationObjectSequence)
                    {
                        int    key = sourceLocationObjectEntry.Key;
                        object sourceLocationObject = sourceLocationObjectEntry.Value;
                        object modelItemObject;

                        if (modelItemObjectSequence.TryGetValue(key, out modelItemObject))
                        {
                            sourceLocationObjectToModelItemObjectMapping.Add(sourceLocationObject, modelItemObject);
                        }
                    }
                }
            }

            this.OnSerializationCompleted(sourceLocationObjectToModelItemObjectMapping);
            return(retVal);
        }