Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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