InternalsVisibleTo does not work
Asked Answered
S

7

77

I insert the line:

[assembly: InternalsVisibleTo("MyTests")]

inside my project under test( Properties/AssemblyInfo.cs) where MyTests is the name of the Unit Test project. But for some reason I still cannot access the internal methods from the unit test project.

Any ideas about what I am doing wrong ?

Somnambulate answered 17/9, 2013 at 9:6 Comment(4)
It's been a looong time but I think the two assemblies have to be signed with the same signature?Detailed
possible duplicate of InternalsVisibleTo attribute isn't workingComeback
I read that on a question in SO but I am not sure if that may create problems in the future. I also couldnt manage to sign my assemblies.Morman
@Cemre: You don't need to sign, if both assemblies are unsigned it should all work. But do read the answers to the linked question.Comeback
D
83

If your assembly is signed with a strong name look at this answer.

Otherwise check that the name of your test assembly really is "MyTests.dll" (it doesn't have to match the project name, though it will by default).

Detailed answered 17/9, 2013 at 9:10 Comment(3)
+1 yep, I remember wasting some time on this issue : the assembly file system name of the test app was different than its project name :)Malevolent
+1 as well. The default class or unit test name is valid, but never what I will have renamed it by the time it gets published. The resulting .dll will retain the first saved file name until the 'Assembly name' is changed in the project's properties.Flavia
When your projects are unsigned also make sure that the application project (the one containing the [assembly: InternalsVisibleTo("MyTests")] friend reference) does not contain [assembly: AssemblyKeyName("")] in its AssemblyInfo.cs file.Niobic
S
16

Let's break it down a bit as many of us have experienced this slight mix-up in the past...

Assembly A has your internal class. Assembly B has your unit tests.

You wish to grant the internals of assembly A visibility in assembly B.

You need to put the InternalsVisibleTo assembly attribute inside assembly A and grant access to assembly B.

Snoopy answered 24/2, 2014 at 10:46 Comment(0)
D
6

You still need your test project to reference your main project.

This can be easy to overlook and if you have no existing test code this may appear like the InternalsVisibleTo is not functioning.

Dekko answered 28/11, 2015 at 22:34 Comment(0)
N
6

I had the same issue after renaming a namespace. Basically the in .cs files was the new namespace but in the .csproj and AssemblyInfo.cs it was the old namespace.

namespace newNamespace {
   ....
}

So, I changed in .csproj the following to the new namespace:

    <RootNamespace>oldnamespace</RootNamespace>
    <AssemblyName>oldnamespace</AssemblyName>

And in AssemblyInfo.cs:

[assembly: AssemblyTitle("oldnamespace")]
[assembly: AssemblyProduct("oldnamespace")]
Nuzzle answered 16/10, 2020 at 7:47 Comment(0)
C
4

In my case I was coding to an Interface. As interfaces can only specify public properties the internal properties were not present on the interface.

Make sure you're not doing the same thing as I was!

I changed:

Assert.IsNotNull((exportFileManager)?.ThumbnailFileExporter);

To:

Assert.IsNotNull((exportFileManager as ExportFileManager)?.ThumbnailFileExporter);
Catadromous answered 18/8, 2017 at 6:20 Comment(3)
Took me forever to find this answer. Cannot understand why the internal explicitly declared implementation of the interface was not recognized,Munford
Explicitly implemented methods are never visible (that's the point); you have to cast the object to the interface in order to get access to those method implementations.Chivalrous
@Chivalrous don't you mean cast the interface to the concrete class? Sure, I understand why it happens but my point it that it's an easy mistake to make perhaps without realising.Catadromous
P
2

I made the incorrect assumption that InternalsVisibleTo will help in accessing Private methods too.

Here are some other aspects that can lead to this error:

  • Your class is either internal or public
  • Your method is internal
  • The InternalsVisibleTo property contains the full namespace of your test project.
Ptyalism answered 29/12, 2022 at 21:43 Comment(3)
My class was public. I can't tell you how long this had me banging my head against the desk.Cate
So the class must be internal not just some of the methods?Perreault
That would surprise me. My class can't be internal or public?Ailsun
B
0

You can expose internals from strong named assembly to another strong named friend assembly only. But non-strong named assembly can expose internals to strong named friend assembly.

Bon answered 7/2, 2020 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.