Is there any function in Roslyn, by which I can parse my string as a MethodDeclarationSyntax?
I have a file which contains the method declaration, so I am reading that file content to string and now I want to create a Method from that string. Any suggestions?
One option is to parse your string as "C# Script", an experimental version of C# that allows top-level method declarations. Doing:
var tree = SyntaxTree.ParseText("void Foo() {}", options: new ParseOptions(kind: SourceCodeKind.Script));
Gives you a valid syntax tree that contains a CompilationUnitSyntax of a single method declaration.
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