2

我正在使用 nrefactory 来修改 c# 代码。我的代码中有各种网络方法。我想在我的 c# 代码中的每个 webmethod 之上设计另一种方法。

示例输入,c# 文件中的原始 C# 方法:

[WebMethod]
public static MoneyTransfer[] GetCities(int StateID, int countryID)
{
....
....
}

样本输出:

private bool validGetCities(int StateID, int countryID)
{
    if (StateID <= 0)
        return false;

    if (countryID <= 0)
        return false;

    return true;
}

[WebMethod]
public static MoneyTransfer[] GetCities(int stateID, int countryID)
{
    if(validGetCities(stateID, countryID))
    {
    ....
    ....
    }
}

在这里,validGetCities() 方法将验证所有输入参数是否为 not <=0 条件。仅当 validGetCities() 返回 true 时才执行 webmethod。我可以使用 nrefactory 编写模式匹配表达式,如下所示,

        var expr = new MethodDeclaration
        {
            Modifiers = Modifiers.Private,
            ReturnType = new PrimitiveType("bool"),
            Name = "validGetCities",

            Body = new BlockStatement{
                new ReturnStatement {
                    Expression = new PrimitiveExpression("true")
                }
            }
        };

此表达式将生成以下代码片段,

private bool validGetCities()
{
    return "true";
}

因此,我可以为 0 个或固定数量的参数生成表达式。我怎样才能使它适用于多个输入参数。

4

0 回答 0