TL;DR Just grab the .snk from the Roslyn repo, name your bridge assembly to something from the InternalsVisibleTo (e.g. Roslyn.Hosting.Diagnostics), sign it with the key, and you can access all the internals at compile time, with IntelliSense!


 

Roslyn has a lot of very useful internal types. In fact, most of the functionality in RoslynPad could not exist without them.

When I started out the ‘Pad, I naturally turned to Reflection to access those types. Even Roslyn’s @jaredpar agrees:

There are things you can’t do even with private Reflection. While you can access existing types, you can’t inherit from an internal class or implement an internal interface. Fortunately, Roslyn’s assemblies have an InternalsVisibleTo to DynamicProxyGenAssembly2, so I used Castle to generate implementations for what I needed. This was cumbersome, but it worked.

However I recently came to realize there’s a far better way: Roslyn’s has a lot of InternalsVisibleTo its test assemblies, and the .snk with the private key is in the public repo. So I just created a bridge assembly, named it to one of the test assemblies, signed it with the .snk, and now I can access all the internal types as if they were public. Super!

 

7 Responses to Simple access to Roslyn’s internals

  1. Nick Polyak says:

    Hey Eli,
    two questions.
    1. I tried what you recommended and still not getting access to internals.
    I called my Assembly Roslyn.Compilers.CSharp.Test.Utilities and used the private key the same as you did for RoslynPad (alternatively I downloaded it from the Roslyn’s master branch). It still was not working.

    2. In your RoslynPad, I do not see any bridge assembly that uses Roslyn names and still miraculously, it picks up the internals.

    Any ideas?

    Thanks in advance.

    • Eli Arbel says:

      Hi Nick,
      In RoslynPad, the assembly I use is `RoslynETAHost`, which allows me to access the assemblies I need (mainly `EditorFeatures`). It should work with any other internals, just check the assembly you need access to has InternalsVisibleTo the test assembly you pick.
      Regarding RoslynPad, you may have been confused because the project name in Visual Studio is named `RoslynPad.Roslyn`.
      Eli.

  2. Nick Polyak says:

    Thanks Eli!
    it does seem to work now.

  3. Nick Polyak says:

    Hey Eli,
    one more question

    1. Will your roslynPad run without VisualStudio installed on the same machine?

  4. Nick Polyak says:

    EditorFeatures is dependent on VisualStudio dlls and because of that the whole project is dependent on them.

    I just tested it.

    • Eli Arbel says:

      Thanks. Only some types in that assembly require VS, and RoslynPad doesn’t use them. I’ll try to remove the dependency in the next release.

Set your Twitter account name in your settings to use the TwitterBar Section.