SixPairs

February 4, 2011

Internal To Public

Filed under: DSL, Reflection — Ceyhun Ciper @ 19:26

Oftentimes, you need to access internal classes of assemblies for which you don’t have the sources. There are many good arguments why you should not do this:

  1. If it is internal, it is for a good reason.
  2. It will probably be modified in a subsequent version.
  3. It has side-effects that you are not aware of.

Argument 1 is weak; if it is not private, it is for a good reason: because it is re-used in some other part of the assembly. Thus argument 2 is weaker, because the assembly is less maintainable by its manufacturer.

On the other hand, there are many hidden jewels in assemblies that we use every day, especially from Microsoft. So some of the other reasons why classes are marked as internal are:

  1. They don’t want to clubber the namespace with unnecessary detail.
  2. They don’t want to write documentation for it; in the case of Microsoft, the MSDN documentation is already very weak, even for over-mature things such as EnvDTE, often consisting of text derived from xml comments giving just the name of a method and the names of its parameters. Imagine if they were to write these comments for non-vital parts of the library, let alone document it (which they have already bypassed).

An example of a useful internal class resides in the Microsoft.Build.dll; it is called SolutionParser and parses any .sln file, for which there is no schema, of course.

So, suppose I have the binaries of the following library:

image

Now suppose I want to access the very much uninteresting property “Property” of the “Class” class. Of course, I have to use reflection and write some code similar to the following:

image

Then I can access the property that I was going after:

var c = new SampleLibrary.Reflected.Class();
var p = c.Property;

As all this is very tedious to do by hand and quite automatic anyway, so I developed a DSL to do it for me; it is posted on the Visual Studio Gallery.

Idea: Create a repository of useful internal classes in Microsoft’s assemblies.

The property might as well be private:

image

Or the class itself:

image

The end result is of course the same.

Here is a screenshot:

image

And here is the screenshot upon conversion:

image

2 Comments »

  1. Could you describe in more detail how to use this? I can’t even find out how to get started(maybe bugged).

    Comment by Jo Blow — April 19, 2011 @ 06:16

  2. You have to add a new “Internal to Public” item to your project (say “foo.internal2public”). If you select a library in the tool window (such as the “SampleLibrary”), you will get a “foo.cs” file with a class in a “whatevernamespace.Reflected” namespace.

    Comment by ceyhunciper — April 25, 2011 @ 05:26


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.