Roslyn - Parse string for MethodDeclarationSyntax

Is there any function in Roslyn with which I can parse my string as MethodDeclarationSyntax?

I have a file that contains a method declaration, so I am reading the contents of this file for a string, and now I want to create a method from this string. Any suggestions?

+4
source share
1 answer

One option is to parse your string as "C # Script", an experimental version of C # that allows you to declare top-level declarations. Performance:

var tree = SyntaxTree.ParseText("void Foo() {}", options: new ParseOptions(kind: SourceCodeKind.Script));

Gives you a valid syntax tree containing CompilationUnitSyntaxdeclarations of a single method.

+4
source

All Articles