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))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With