So, I'm trying to find out the type of some variables in an argument when calling a method. For instance:
class Program { static void Main(string[] args) { string a = "astring" string.Format("zzzz{0}", a); } }
So, I want to know the type of the variable a in string.Format ("zzzz {0}, a"); I am trying to change the code, so I am using a rewriter, and this is what I got:
public class CustomFormatRewriter : CSharpSyntaxRewriter{ public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax rootNode){ if (null != rootNode){ var arguments = from n in secondNode.DescendantNodes() where (n.Kind() == SyntaxKind.Argument) select n; foreach (var argument in arguments) { var compilation = CSharpCompilation.Create("arg") .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)) .AddSyntaxTrees(rootNode.SyntaxTree); var model = compilation.GetSemanticModel(rootNode.SyntaxTree); var symbolInfo = model.GetSymbolInfo(argument); Console.WriteLine(symbolInfo.Symbol); } } return rootNode; } }
But the symbol.Symbol symbol that I received is null.
Any idea? Thanks!!
source share