次の方法で共有


System.Runtime.CompilerServices.InternalsVisibleTo works

This is a very sneaky feature which has been discovered or blogged by quite a few .net gurus.  I just want to give a simple example as how to declare it properly.  Let us say you are developing a core assembly called MyAssembly.  And you also developed another unit test assembly called MyAssembly.UnitTest.  In any class of MyAssembly, you can add something like

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyAssembly.UnitTest")]

Note here this assembly attribute has to be declared outside your own namespace. For example, the following declaration will give you compilation error.

namespace MyAssemblyNamespace

{

      [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyAssembly.UnitTest")]

      public class A

     {

         blah...

      }

}

Compiler will give you the following error

     assembly is not a valid attribute location for this declaration, valid attribute location is type.

Now if you move the attribute outside your own namespace, it will start to work

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyAssembly.UnitTest")]

namespace MyAssemblyNamespace

{

      public class A

     {

         blah...

      }

}

This is a really cool Whidbey feature.  Unit testing your internal classes and methods is just one attribute away!