Write a method for webmethod in C# using nrefactory

112 Views Asked by At

I'm using nrefactory to modify the c# code. I have various web methods in my code. I want to design an another method above every webmethod in my c# code.

Sample Input, Original C# Method in c# file:

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

Sample output:

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))
    {
    ....
    ....
    }
}

Here, method validGetCities() will validate the all input paramters for not <=0 condtion. Execute webmethod only if validGetCities() returns true. I could write pattern matching expression using nrefactory as below,

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

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

This Expression will generate following code snippet,

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

So, I could generate expression for 0 or fixed number of parameters. How can i make it work for multiple input parameters.

0

There are 0 best solutions below