InternalsVisibleTo seems ignored

I am trying to unit test a private function in .net. This private function returns a collection of type myClass, which is an inner class.

I used the assembly attribute InternalsVisibleTo, so the type myClassis known to my test project.

Here is the code I want to check:

namespace MyProject
{
    public class Class1
    {
         private List<myClass> myFunction()
         {
             return new List<myClass>();
         }

         internal class myClass
         {
             public int MyProperty { get; set; }
         }   
     }
 }

[TestMethod()]
[DeploymentItem("MyProject.dll")]
public void myFunctionTest()
{
    Class1_Accessor target = new Class1_Accessor(); 
    List<Class1_Accessor.myClass> expected = null; 
    List<Class1_Accessor.myClass> actual;
    actual = target.myFunction();
    Assert.AreEqual(expected, actual);
    Assert.Inconclusive("Verify the correctness of this test method.");
}

and in my assembly information file:

[assembly: InternalsVisibleTo("MyProject.Test")]

So why is Visual Studio setting the list type Class1_Accessor.myClassas it myClassis known to my test project?

Because of this, I get a runtime error (cannot convert type myClassto type Class1_Accessor.myClass).

Since myFunction is private, VisualStudio generates the following code (which works great for most)

[Shadowing("MyProject.Class1")]
public class Class1_Accessor : BaseShadow
{
    protected static PrivateType m_privateType;

    [Shadowing(".ctor@0")]
    public Class1_Accessor();
    public Class1_Accessor(PrivateObject value);

    public static PrivateType ShadowedType { get; }

    public static Class1_Accessor AttachShadow(object value);
    [Shadowing("myFunction@0")]
    public List<Class1_Accessor.myClass> myFunction();

    [Shadowing("MyProject.Class1+myClass")]
    public class myClass : BaseShadow
    {
        protected static PrivateType m_privateType;

        [Shadowing(".ctor@0")]
        public myClass();
        public myClass(PrivateObject value);

        [Shadowing("MyProperty")]
        public int MyProperty { get; set; }
        public static PrivateType ShadowedType { get; }

        public static Class1_Accessor.myClass AttachShadow(object value);
    }
}

, myClass, , . , .

+5
2

myClass Class1_Accessor.myClass

, : - myClass. ; [InternalsVisibleTo(...)] , : .

, / myClass :

  • disambiguate (namespace-qualify ..)
  • , , .

, :

namespace A { class Foo {} }
namespace B { class Foo {} }

, Foo, .

+4

InternalsVisibleTo . /, . .

( doc): , Unit Test "". , " "

0

All Articles