12

Below is the function I'm using to generate a simple method -

//NOTE : SF = SyntaxFactory
        List<ParameterSyntax> parameterList = new List<ParameterSyntax>
        {
            SF.Parameter(SF.Identifier(sourceObjectName))
        };
        // Create method
        var method = SF.MethodDeclaration(SF.ParseName(destinationClass), functionName)
            .WithBody(SF.Block(nodes))  
            .AddModifiers(SF.Token(SyntaxKind.PublicKeyword))
            .AddParameterListParameters(parameterList.ToArray())
            .NormalizeWhitespace();
        // NEED TO ADD PARAMS TO CODE
        Console.WriteLine(method.GetText());

And here's the output:

public XYZ MapABCToXYZ(fromObject) // Should be 'ABC fromObject'
{
    XYZ myObject = new XYZ();
    myObject.MyProperty = fromObject.MyProperty;
    myObject.TestProperty = fromObject.TestProperty;
    return myObject;
}

As you can see, the parameter is not "ABC fromObject" and I've been trying to figure out the exact syntax to add parameters properly.

I've tried various ways to figure out the parameter syntax and have come up blank mostly.

EDIT: Figured it out. Just had to make a change in the following line:

SF.Parameter(SF.Identifier(sourceObjectName)).WithType(SF.ParseTypeName(sourceClass))
2
  • 2
    You should post your solution as an answer and then accept it, instead of editing your question.
    – svick
    Apr 22, 2015 at 11:24
  • Oh okay. Will do that. Didn't know. Apr 23, 2015 at 12:27

1 Answer 1

8

As suggested, I'm posting the solution here -

Figured it out. Just had to make a change in the following line:

SF.Parameter(SF.Identifier(sourceObjectName)).WithType(SF.ParseTypeName(sourceClass))

Where 'sourceClass' is a string of the required type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.