Skip to content

Instantly share code, notes, and snippets.

@JoshVarty
Last active August 29, 2015 14:14
Show Gist options
  • Save JoshVarty/df98e5094d4ca7e98b91 to your computer and use it in GitHub Desktop.
Save JoshVarty/df98e5094d4ca7e98b91 to your computer and use it in GitHub Desktop.
var tree = CSharpSyntaxTree.ParseText(@"
class MyClass
class MyClass
{
int myField1, myField2, myField3;
}");
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var field = tree.GetRoot().DescendantNodes().OfType<FieldDeclarationSyntax>().Single();
foreach (var variable in field.Declaration.Variables)
{
//Now we can access each of the symbols within the field
var fieldSymbol = model.GetDeclaredSymbol(variable);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment