【问题标题】:Given a TFS build definition how to get the value and type of an arbitrary process parameter?给定 TFS 构建定义如何获取任意进程参数的值和类型?
【发布时间】:2015-09-22 16:16:11
【问题描述】:

给定一个构建定义,我从中提取以下部分:

m_template = (DynamicActivity)WorkflowHelpers.DeserializeWorkflow(buildDefinition.Process.Parameters);
Properties = m_template.Properties.ToDictionary(p => p.Name, StringComparer.OrdinalIgnoreCase);
Metadata = WorkflowHelpers.GetCombinedMetadata(m_template).ToDictionary(m => m.ParameterName);
m_parameters = WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters)

现在我想知道任意过程参数的值。

我当前的代码是:

public ParameterValue GetParameterValue(string name)
{
    object propValue;
    var valueType = GetParameterType(name, out propValue);

    object value;
    if (!m_parameters.TryGetValue(name, out value))
    {
        value = propValue;
    }

    return new ParameterValue(valueType, value);
}

private Type GetParameterType(string name, out object value)
{
    value = null;
    if (Properties != null)
    {
        DynamicActivityProperty property;
        if (Properties.TryGetValue(name, out property))
        {
            var inArgument = property.Value as InArgument;
            if (inArgument != null)
            {
                if (inArgument.Expression != null)
                {
                    var exprString = inArgument.Expression.ToString();
                    if (!exprString.StartsWith(": VisualBasicValue<"))
                    {
                        value = exprString;
                    }
                }
                return inArgument.ArgumentType;
            }
            if (property.Value != null)
            {
                value = property.Value;
                return property.Value.GetType();
            }
            var typeName = property.Type.ToString();
            if (typeName.StartsWith(IN_ARGUMENT_TYPE_NAME_PREFIX))
            {
                typeName = typeName.Substring(IN_ARGUMENT_TYPE_NAME_PREFIX.Length, typeName.Length - IN_ARGUMENT_TYPE_NAME_PREFIX.Length - 1);
                return Type.GetType(typeName, true);
            }
            return property.Type;
        }
    }

    return typeof(string);
}

不幸的是,这段代码在寻找满足以下所有条件的参数时遇到了问题:

  • 参数值包装为InArgument&lt;T&gt;。
  • T 是非原始类型,例如string[]
  • 构建定义不会覆盖从流程模板继承的值。

会发生什么:

  1. 因为值是非原始的exprString.StartsWith(": VisualBasicValue&lt;"),我不知道如何处理。因此propValue 是null。
  2. 因为该值未被构建定义!m_parameters.TryGetValue(name, out value) 覆盖,因此我只返回propValue。

结果我的逻辑返回null。但这是错误的!例如,我有一个string[] 参数,它在流程模板中有一个字符串列表,但我的逻辑返回null,原因已解释。

那么,计算它的正确方法是什么?

【问题讨论】:

    标签: tfs workflow


    【解决方案1】:

    您可以使用以下代码(包含在另一个link 中)来获取一个过程参数的值和类型:

      TfsTeamProjectCollection tfctc = new TfsTeamProjectCollection(new Uri("http://tfsservername:8080/tfs/DefaultCollection"));
            IBuildServer bs = tfctc.GetService<IBuildServer>();
    
            IBuildDetail[] builds = bs.QueryBuilds("teamprojectname", "builddefinitionname");
            foreach (var build in builds)
            {
                var buildefinition = build.BuildDefinition;
                IDictionary<String, Object> paramValues = WorkflowHelpers.DeserializeProcessParameters(buildefinition.ProcessParameters);
                string processParametersValue = paramValues["argument1"].ToString();
                Console.WriteLine(processParametersValue);
            }
    

    对此案也有检查:TFS 2010: Why is it not possible to deserialize a Dictionary<string, object> with XamlWriter.Save when I can use XamlReader for deserializing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2014-09-06
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多