public void FindAllInvocationsToMethodsFromAParticularNamespace()
{
var tree = SyntaxTree.ParseText(@"
using System;
using System.Threading.Tasks;
class Program
{
static void Main()
{
Action a = () => {};
var t = Task.Factory.StartNew(a);
t.Wait();
Console.WriteLine(a.ToString());
a = () =>
{
t = new Task(a);
t.Start();
t.Wait();
};
a();
}
}");
var compilation = Compilation.Create("MyCompilation")
.AddReferences(MetadataReference.CreateAssemblyReference("mscorlib"))
.AddSyntaxTrees(tree);
var model = compilation.GetSemanticModel(tree);
// Instantiate MethodInvocationWalker (below) and tell it to find invocations to methods from the System.Threading.Tasks namespace.
var walker = new MethodInvocationWalker()
{
SemanticModel = model,
Namespace = "System.Threading.Tasks"
};
walker.Visit(tree.GetRoot());
Assert.AreEqual(@"
Line 8: Task.Factory.StartNew(a)
Line 9: t.Wait()
Line 14: new Task(a)
Line 15: t.Start()
Line 16: t.Wait()", walker.Results.ToString());
}