The Problem SolverWIndows Workflow Foundation
HomeShow ChangesEditPrintRecent ChangesSubscriptionsLost and FoundFind ReferencesRename

Search

History

1-11-2006 16:34:19

List all versions

Xaml And Generics
.
Summary

Version 1 of Worflow Foundation doesn't support loading generic types through XAML.

Tom Lake posted the following code in http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=628765&SiteID=1 as a temporary workaround for the problem.

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<Parameter> parameters = (List<Parameter>)value;
        StringBuilder stringValue = new StringBuilder();
 
        foreach (Parameter parameter in parameters)
        {
            stringValue.Append(string.Format("{0}:{1},", parameter.Name, parameter.Type.ToString()));
        }
 
        if (stringValue.Length > 0)
        {
            stringValue.Remove(stringValue.Length - 1, 1);
        }
 
        return stringValue.ToString();
    }
 
    protected override object DeserializeFromString(WorkflowMarkupSerializationManager serializationManager, Type propertyType, string value)
    {
        List<Parameter> paramList = new List<Parameter>();
        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();
                }
                Parameter parameter = new Parameter();
                parameter.Name = paramValues[0]; 
                switch(paramValues[1])
                {
                    case "Integer":
                        parameter.Type = ParameterType.Integer;
                        break;
                    case "DateTime":
                        parameter.Type = ParameterType.DateTime;
                        break;
                    case "String":
                        parameter.Type = ParameterType.String;
                        break;
                }
                paramList.Add(parameter);                 
            }
        }
 
        return paramList;
    }
 
    #region IDesignerSerializationProvider Members
 
    public object GetSerializer(IDesignerSerializationManager manager, object currentSerializer, Type objectType, Type serializerType)
    {
        if (objectType == typeof(List<Parameter>))
        {
            return this;
        }
        else
        {
            return null;
        }
    }
    #endregion
}

And then add the following attribute to the DynamicPropertiesActivity 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