The Problem SolverWIndows Workflow Foundation
HomeShow ChangesEditPrintRecent ChangesSubscriptionsLost and FoundFind ReferencesRename

Search

History

27-10-2006 12:03:46

List all versions

Designer Serializer Attribute
.
Summary

DesignerSerializerAttribute provides a way to indicate to the designer serialization manager that a specific type of serializer should be used when serializing the values of an object. This allows you to specify a serializer that is capable of serializing a custom type, for instance. Place this attribute on a class to indicate the serialization object to use when serializing the class.

One reason to use tis might be with generic parameters, something not supposted in XOML workflows. Use a custom serializer to enable this.

Taken from: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=554070&SiteID=1

using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Text;

namespace Microsoft.Workflow.Samples.WebPage
{
    public class ListSerializer : WorkflowMarkupSerializer, IDesignerSerializationProvider
    {
        protected override bool CanSerializeToString(WorkflowMarkupSerializationManager serializationManager, object value)
        {
            return true;
        }

        protected override void OnBeforeSerialize(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            serializationManager.AddSerializationProvider(this);
            base.OnBeforeSerialize(serializationManager, obj);
        }

        protected override void OnAfterSerialize(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            base.OnAfterSerialize(serializationManager, obj);
            serializationManager.RemoveSerializationProvider(this);
        }

        protected override void OnBeforeDeserialize(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            serializationManager.AddSerializationProvider(this);
            base.OnBeforeDeserialize(serializationManager, obj);
        }

        protected override void OnAfterDeserialize(WorkflowMarkupSerializationManager serializationManager, object obj)
        {
            base.OnAfterDeserialize(serializationManager, obj);
            serializationManager.RemoveSerializationProvider(this);
        }

        protected override string SerializeToString(WorkflowMarkupSerializationManager serializationManager, object value)
        {
            List<PageParameter> parameters = (List<PageParameter>)value;
            StringBuilder stringValue = new StringBuilder();
            foreach (PageParameter parameter in parameters)
                stringValue.Append(string.Format("{0}:{1},", parameter.Name, parameter.Value));

            if (stringValue.Length > 0)
                stringValue.Remove(stringValue.Length - 1, 1);

            return stringValue.ToString();
        }

        protected override object DeserializeFromString(WorkflowMarkupSerializationManager serializationManager, Type propertyType, string value)
        {
            List<PageParameter> paramList = new List<PageParameter>();
            string stringValue = value as string;

            if (stringValue != string.Empty)
            {
                string[] parameters = stringValue.Split(',');
                for (int i = 0; i < parameters.Length; ++i)
                {
                    string[] paramValues = parametersIdea.Split(':');
                    if (paramValues.Length != 2)
                    {
                        throw new InvalidOperationException();
                    }

                    PageParameter parameter = new PageParameter();
                    parameter.Name = paramValues[0];
                    parameter.Value = paramValues[1];
                    paramList.Add(parameter);
                }
            }

            return paramList;
        }

        #region IDesignerSerializationProvider Members

        public object GetSerializer(IDesignerSerializationManager manager, object currentSerializer, Type objectType, Type serializerType)
        {
            if (objectType == typeof(List<PageParameter>))
                return this;
            else
                return null;
        }
        #endregion
    }
}

Then add the following attribute on top of WebPageRequest/WebPageResponse class

[DesignerSerializer(typeof(ListSerializer), typeof(WorkflowMarkupSerializer))]
Wiki Usage

This wiki site is supposed to be a shared resource. As a shared resource everyone is encouraged to add new content or modify existing content!

Enjoy the WF wiki.

Recent Topics