Beispiel #1
0
        XamlType GetCustomType(Type type)
        {
            if (type == typeof(DesignerAttribute))
            {
                return(new AttributeXamlType <DesignerAttribute, DesignerAttributeInfo>(this));
            }
            if (type == typeof(EditorAttribute))
            {
                return(new AttributeXamlType <EditorAttribute, EditorAttributeInfo>(this));
            }
            if (type == typeof(DefaultValueAttribute))
            {
                return(new AttributeXamlType <DefaultValueAttribute, DefaultValueAttributeInfo>(this));
            }
            if (type.Namespace == "System.ComponentModel.Composition")
            {
                return(GetCustomMefType(type));
            }
#if ERROR_TOLERANT_SUPPORT
            if (ErrorTolerantObjectWriter.IsErrorActivity(type))
            {
                return(new ShimAsPublicXamlType(type, this));
            }
#endif
            return(null);
        }
        private object DeserializeString(string text, DeserializationMode mode, out IList <XamlLoadErrorInfo> loadErrors, out Dictionary <object, SourceLocation> sourceLocations)
        {
            object result = null;

            loadErrors = null;
            Dictionary <object, SourceLocation> collectingSourceLocations   = new Dictionary <object, SourceLocation>(ObjectReferenceEqualityComparer <object> .Default);
            SourceLocationFoundCallback         sourceLocationFoundCallback = new SourceLocationFoundCallback((obj, sourceLocation) =>
            {
                // If an object appear more than once in the XAML stream (e.g. System.Type, which is cached by reflection)
                // we count the first occurrence.
                if (!collectingSourceLocations.ContainsKey(obj))
                {
                    collectingSourceLocations.Add(obj, sourceLocation);
                }

                this.OnSourceLocationFound(obj, sourceLocation);
            });

            this.XamlSchemaContext.ContainsConversionRequiredType = false;
            Dictionary <string, SourceLocation> viewStateDataSourceLocationMapping = null;

            using (XamlDebuggerXmlReader debuggerReader = new XamlDebuggerXmlReader(new StringReader(text), this.XamlSchemaContext))
            {
                using (System.Xaml.XamlReader activityBuilderReader = ActivityXamlServices.CreateBuilderReader(debuggerReader))
                {
                    using (System.Xaml.XamlReader activityTemplateFactoryBuilderReader = new ActivityTemplateFactoryBuilderReader(activityBuilderReader, this.XamlSchemaContext))
                    {
                        debuggerReader.SourceLocationFound += delegate(object sender, SourceLocationFoundEventArgs args)
                        {
                            sourceLocationFoundCallback(args.Target, args.SourceLocation);
                        };

                        this.OnBeforeDeserialize();
                        debuggerReader.CollectNonActivitySourceLocation = this.FrameworkName.Is45OrHigher();

                        using (System.Xaml.XamlReader reader = ViewStateXamlHelper.ConvertViewStateToAttachedProperties(activityTemplateFactoryBuilderReader, this.IdManager, out viewStateDataSourceLocationMapping))
                        {
                            switch (mode)
                            {
#if ERROR_TOLERANT_SUPPORT
                            case DeserializationMode.ErrorTolerant:
                            {
                                ErrorTolerantObjectWriter tolerantWriter = new ErrorTolerantObjectWriter(reader.SchemaContext);
                                tolerantWriter.LocalAssemblyName = this.LocalAssemblyName;
                                XamlServices.Transform(reader, tolerantWriter);
                                loadErrors = this.CheckFileFormatError(tolerantWriter.LoadErrors);
                                result     = tolerantWriter.Result;
                                ErrorActivity.SetHasErrorActivities(result, true);
                            }

                            break;
#endif
                            case DeserializationMode.Default:
                            {
                                result = this.TransformAndGetPropertySourceLocation(reader, new SourceTextScanner(text), sourceLocationFoundCallback);

                                loadErrors = this.CheckFileFormatError(loadErrors);
                            }

                            break;
                            }
                        }
                    }
                }
            }

            sourceLocations = collectingSourceLocations;
            this.OnAfterDeserialize(viewStateDataSourceLocationMapping);
            return(result);
        }
        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);
        }