0

I am creating a code extractor/inlining tool that will make it easier for me to share snippets of source code. It will ideally take a method that I provide, walk the method's syntax tree looking for references to methods/types in my other assemblies, and extract them all into a single block of code. This way, if I want to share an extension method I wrote that references other extensions that reference other extensions, I don't have to painstakingly compile it all into something I can copy and paste on the internet.

This article got me started:

https://joshvarty.com/learn-roslyn-now/

And below is what I have so far. I have an extension method called TrimWhiteSpace() in a GeneralUtilities class in the MyExtensions namespace. The below code successfully identifies this extension, but I also want access to the method's body so that I can walk the syntax tree there looking for other extensions. Because I am using the DLL directly, it won't have access to the body, so I need to generate a SemanticModel using the project instead. Using the project path doesn't appear to work.

public static void SemanticsTest()
{
    var tree = CSharpSyntaxTree.ParseText(@"using MyExtensions;
    namespace TestNamespace
    {
    public static class TestClass
    {
        public static void CallTrimWhiteSpace()
        {
            var x = "" some string  "".TrimWhiteSpace();
        }
    }
    }");

    var root = tree.GetRoot();

    var assemblyPath = MetadataReference.CreateFromFile(typeof(GeneralUtilities).Assembly.Location);

    var compilation = CSharpCompilation.Create("MyCompilation",
        syntaxTrees: new[] { tree }, references: new[] { assemblyPath });

    var model = compilation.GetSemanticModel(tree);

    var expression = root.DescendantNodes().OfType<InvocationExpressionSyntax>().FirstOrDefault();
    var symbol = model.GetSymbolInfo(expression);
}
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
  • Did you tried to use Workspaces to load project? https://www.youtube.com/watch?v=35BFJt91rxY&list=PLxk7xaZWBdUT23QfaQTCJDG6Q1xx6uHdG&index=7 – dropoutcoder Sep 06 '19 at 01:38
  • @dropoutcoder I began to dabble in Workspaces, but hit a brick wall after seeing that `MSBuildWorkspace` was unavailable for some reason. Rather than go down a rabbit hole of uncertainty, I just posted my question here. But if workspaces is the direction I need to go then I'll take a deeper look into why a reference to that class can't be located. – oscilatingcretin Sep 06 '19 at 01:43
  • There is not just MSBuildWorkspace. There is also VisualStudioWorkspace and AdHocWorkspace. All three have different reasons to exist. Anyway, you'll have a lot of things to deal with to make it work properly, but I thing it is doable. – dropoutcoder Sep 06 '19 at 01:49
  • First of all you'll need to load VisualStudioWorkspace to get access to your current solution. If you are planning to create/extract source code only from projects inside the loaded solution it might be just some recursive walk until you get somewhere you can't go. External libs. As I said doable and worth trying. Great idea! – dropoutcoder Sep 06 '19 at 01:52
  • @dropoutcoder Thanks. I just got `MSBuildWorkspace` working, so now I'll look into the other types of workspaces. – oscilatingcretin Sep 06 '19 at 01:56

0 Answers0