KevinUp

real world programming

InternalsVisibleTo

Posted by kevinup on August 6, 2007

So I was at .net user group the other day and someone said off handedly, “You can expose internal methods to other assemblies.” I thought at the time, why would you want to do that? Isn’t that the point of making a method internal in the first place.

Fast forward to later. I was writing a static method, that was originally private. Via refactoring another class in the assembly, I was able to remove a bunch of code, but changed the scope of my original function to ‘internal.’ My tests were still passing in all paths through my internal function, and I still had 100% coverage. Then I thought to myself. What I really wanted to do at this point, was write some tests around my new ultra cool internal method. But it went against every thing I believe in just to change the scope of my method to public, just to write some tests on it. Then I remembered user group, and went looking for it and walla:

[assembly: InternalsVisibleTo("BusinessLogic.Tests")]

I dropped this into my AssemblyInfo.cs, and now I can test internal methods from my testing framework. I also went refactoring through some other functions, and was able to change their scope to internal as well. And of course, intellisense works.

Advertisements

2 Responses to “InternalsVisibleTo”

  1. Jayme said

    Sweet! I started off thinking the same thing you did… “why would someone do this?!?!?” … but now it makes sense.

  2. Dave said

    So, what version of visual studio are you working with? ‘Cause this I’m using 2008 professional and I can’t seem to make Intellisense work on those internal methods… Is there something additional I have to do? ‘Cause simply adding that attribute to my AssemblyInfo.cs doesn’t seem to enable intellisense for those internal classes and methods…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: