Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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);
Copy link

vikas379 commented Feb 10, 2020

Hi @JoshVarty,
Line no. 16 should have MethodDeclarationSyntax instead of PropertyDeclarationSyntax


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment