Note that there are some explanatory texts on larger screens.

plurals
  1. POInternalsVisibleTo seems ignored
    primarykey
    data
    text
    <p>I'm trying to unit test a private function in .net. This private function returns a collection of type <code>myClass</code>, which is an internal class. </p> <p>I've used the assembly attribute <code>InternalsVisibleTo</code>, so that the type <code>myClass</code> is known to my Test project.</p> <p>Here's the code I want to test:</p> <pre><code>namespace MyProject { public class Class1 { private List&lt;myClass&gt; myFunction() { return new List&lt;myClass&gt;(); } internal class myClass { public int MyProperty { get; set; } } } } [TestMethod()] [DeploymentItem("MyProject.dll")] public void myFunctionTest() { Class1_Accessor target = new Class1_Accessor(); List&lt;Class1_Accessor.myClass&gt; expected = null; List&lt;Class1_Accessor.myClass&gt; actual; actual = target.myFunction(); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); } </code></pre> <p>and in my assembly info file:</p> <pre><code>[assembly: InternalsVisibleTo("MyProject.Test")] </code></pre> <p>So why does Visual Studio set the type of the list to <code>Class1_Accessor.myClass</code> since <code>myClass</code> is known to my test project ? </p> <p>Because of that I get a runtime error (cannot convert type <code>myClass</code> to type <code>Class1_Accessor.myClass</code>).</p> <p>Because myFunction is private, VisualStudio generates the following code (which is fine for most of it)</p> <pre><code>[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&lt;Class1_Accessor.myClass&gt; 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); } } </code></pre> <p>However, I don't understand why it contains a new definition of myClass, since it is an internal class, which shouldn't need any accessor. This is the root of the problem in my opinion.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload