如何获取变量和分配/默认值列表?

时间:2017-03-28 17:33:00

标签: xaml workflow-foundation-4 workflow-foundation

我们需要经历一系列Microsoft Windows工作流程(.xaml文件)并清点变量。工作流程很复杂,变量范围很多,所以我不能简单地打开Workflow xaml并查看顶层的Variables选项卡;我需要挖掘每个级别,序列等,以找到所有可能的变量定义。

我可以自动执行此过程吗? Visual Studio可以帮助完成此过程吗?

一个解决方案,我可以编写一些代码来读取工作流文件,查找变量,获取任何默认值,并检查变量是否已分配,从而覆盖默认值。从技术上讲,这可以从C#中获得。但这个解决方案真的有必要获取信息吗?

1 个答案:

答案 0 :(得分:0)

您可以使用这样的递归函数:

List<Variable> Variables;

private void GetVariables(DynamicActivity act)
{
    Variables = new List<Variable>();
    InspectActivity(act);
}

private void InspectActivity(Activity root)
{
    IEnumerator<Activity> activities = WorkflowInspectionServices.GetActivities(root).GetEnumerator();

    while (activities.MoveNext())
    {
        PropertyInfo propVars = activities.Current.GetType().GetProperties().FirstOrDefault(p => p.Name == "Variables" && p.PropertyType == typeof(Collection<Variable>));

        if (propVars != null)
        {
            try
            {
                Collection<Variable> variables = (Collection<Variable>)propVars.GetValue(activities.Current, null);

                variables.ToList().ForEach(v =>
                {
                    Variables.Add(v);
                });
            }
            catch
            {

            }
        }

        InspectActivity(activities.Current);
    }
}

应该像这样调用:

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xamlData)))
{
    XamlXmlReaderSettings readerSettings = new XamlXmlReaderSettings()
    {
        LocalAssembly = Assembly.GetExecutingAssembly()
    };

    var xamlReader = new XamlXmlReader(stream, readerSettings);

    Activity activity = ActivityXamlServices.Load(xamlReader);

    DynamicActivity root = activity as DynamicActivity;
    GetVariables(root);
}

感谢:https://stackoverflow.com/a/11429284/593609