Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Stevie-O/06ac50f4f219d2e1e50e5eabd200ed4b to your computer and use it in GitHub Desktop.
Save Stevie-O/06ac50f4f219d2e1e50e5eabd200ed4b to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<NuGetReference>Microsoft.Build</NuGetReference>
<NuGetReference>Microsoft.Build.Framework</NuGetReference>
<NuGetReference>Microsoft.Build.Tasks.Core</NuGetReference>
<NuGetReference>Microsoft.Build.Utilities.Core</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.CSharp</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.CSharp.Workspaces</NuGetReference>
<NuGetReference>Microsoft.CodeAnalysis.Workspaces.MSBuild</NuGetReference>
<NuGetReference>NuGet.Build.Tasks</NuGetReference>
<NuGetReference>System.ValueTuple</NuGetReference>
<Namespace>Microsoft.CodeAnalysis</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp.Syntax</Namespace>
<Namespace>Microsoft.CodeAnalysis.FindSymbols</Namespace>
<Namespace>Microsoft.CodeAnalysis.MSBuild</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>Microsoft.CodeAnalysis.Options</Namespace>
<Namespace>Microsoft.CodeAnalysis.CSharp</Namespace>
</Query>
void Main()
{
var sln = CreateTestSolution();
var prj = sln.Projects.First();
var comp = prj.GetCompilationAsync().Result;
var idriveable = comp.GetTypeByMetadataName("IDriveable");
var idriveable_drive = idriveable.GetMembers("Drive").Single();
var motorcycle = comp.GetTypeByMetadataName("Motorcycle");
var motorcycle_drive = motorcycle.GetMembers("Drive").Single();
SymbolFinder.FindReferencesAsync(motorcycle_drive, sln).Result.Dump("FindReferencesAsync for " + motorcycle_drive);
SymbolFinder.FindCallersAsync(motorcycle_drive, sln).Result.Dump("FindCallersAsync for " + motorcycle_drive);
SymbolFinder.FindReferencesAsync(idriveable_drive, sln).Result.Dump("FindReferencesAsync for " + idriveable_drive);
SymbolFinder.FindCallersAsync(idriveable_drive, sln).Result.Dump("FindCallersAsync for " + idriveable_drive);
}
Solution CreateTestSolution()
{
var projname = Util.CurrentQuery.Name;
var projid = ProjectId.CreateNewId(projname);
var ws = new AdhocWorkspace();
AppDomain.CurrentDomain.GetAssemblies().Where(s => s.FullName.StartsWith("Microsoft")).Select(s => s.ToString()).Dump("Roslyn Version");
var sln = ws.CurrentSolution
.AddProject(projid, projname, projname, LanguageNames.CSharp)
.AddMetadataReference(projid, MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
.AddDocument(DocumentId.CreateNewId(projid, "main.cs"),
"main.cs",
@"using System;
public interface IDriveable { void Drive(); }
public class Vehicle : IDriveable { public virtual void Drive() {} }
public class Motorcycle : Vehicle, IDriveable
{
public override void Drive() {}
void IDriveable.Drive() { }
}
public class Tests
{
public static void TestMotorcycle()
{
var m = new Motorcycle();
m.Drive();
}
public static void TestDriveable(IDriveable d)
{
d.Drive();
}
}
")
;
if (!ws.TryApplyChanges(sln)) throw new Exception("Cannot apply changes");
return sln;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment