如何获得变量和赋值/默认值的列表?

How can I get a list of variables and assignments / default values?

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

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

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

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

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);
}

感谢: