Last active
February 10, 2020 10:05
-
-
Save JoshVarty/1c33e136ccc9776692b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tree = CSharpSyntaxTree.ParseText(@" | |
class MyClass | |
{ | |
int myField = 0; | |
public int MyProperty {get; set;} | |
public void MyMethod() { } | |
}"); | |
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly); | |
var compilation = CSharpCompilation.Create("MyCompilation", | |
syntaxTrees: new[] { tree }, references: new[] { Mscorlib }); | |
var model = compilation.GetSemanticModel(tree); | |
//Get declarations | |
var property = tree.GetRoot().DescendantNodes().OfType<PropertyDeclarationSyntax>().Single(); | |
var method = tree.GetRoot().DescendantNodes().OfType<PropertyDeclarationSyntax>().Single(); | |
var field = tree.GetRoot().DescendantNodes().OfType<FieldDeclarationSyntax>().Single(); | |
//Get symbols | |
var propertySymbol = model.GetDeclaredSymbol(property); | |
var methodSymbol = model.GetDeclaredSymbol(method); | |
var fieldSymbol = model.GetDeclaredSymbol(field); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @JoshVarty,
Line no. 16 should have MethodDeclarationSyntax instead of PropertyDeclarationSyntax
Cheers,
Vikas