Skip to content

Instantly share code, notes, and snippets.

Embed
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