Questions tagged [internalsvisibleto]

An attribute in .NET to expose members marked as internal to selected assemblies.

In .NET, members with the access modifier 'internal' are only visible to classes inside the current assembly. This can be circumvented by adding the InternalsVisibleTo attribute to the AssemblyInfo file.

This way, the assembly with the internal members can control which other assemblies have access to these members.

Both assemblies must be unsigned or both must be signed. If both are signed, the full public key must be included in the constructor of the attribute.

An unsigned assembly can expose the internal members to another unsigned assembly with (C# and VB.NET):

[assembly:InternalsVisibleTo("AnUnsignedAssembly")]
<Assembly:InternalsVisibleTo("AnUnsignedAssembly")>

For a signed assembly to expose the internal members to another signed assembly, the following can be used (C# and VB.NET):

[assembly: InternalsVisibleTo("SignedAssembly, PublicKey=002400000480000094" + 
                          "0000000602000000240000525341310004000" +
                          "001000100bf8c25fcd44838d87e245ab35bf7" +
                          "3ba2615707feea295709559b3de903fb95a93" +
                          "3d2729967c3184a97d7b84c7547cd87e435b5" +
                          "6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" +
                          "712da72eec2533dc00f8529c3a0bbb4103282" +
                          "f0d894d5f34e9f0103c473dce9f4b457a5dee" +
                          "fd8f920d8681ed6dfcb0a81e96bd9b176525a" +
                          "26e0b3")]
<Assembly:InternalsVisibleTo("SignedAssembly, PublicKey=002400000480000094" + _
                         "0000000602000000240000525341310004000" + _
                         "001000100bf8c25fcd44838d87e245ab35bf7" + _
                         "3ba2615707feea295709559b3de903fb95a93" + _
                         "3d2729967c3184a97d7b84c7547cd87e435b5" + _
                         "6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" + _
                         "712da72eec2533dc00f8529c3a0bbb4103282" + _
                         "f0d894d5f34e9f0103c473dce9f4b457a5dee" + _
                         "fd8f920d8681ed6dfcb0a81e96bd9b176525a" + _
                         "26e0b3")>
71 questions
82
votes
24 answers

InternalsVisibleTo attribute isn't working

I am trying to use the InternalsVisibleTo assembly attribute to make my internal classes in a .NET class library visible to my unit test project. For some reason, I keep getting an error message that says: 'MyClassName' is inaccessible due to its…
skb
  • 30,624
  • 33
  • 94
  • 146
72
votes
7 answers

InternalsVisibleTo does not work

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…
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
20
votes
3 answers

Cannot create a Mock class for an internal type using Rhino Mocks

I am using Rhino Mocks as a mocking framework for unit testing. I have a class called Subject which is the class I want to test. It has a dependency on IStore. IStore is defined as follows: //internal interface : has InternalsVisible to both…
Santhosh
  • 6,547
  • 15
  • 56
  • 63
17
votes
5 answers

Using "friend"-declarations for unit testing. Bad idea?

[Of course, the question is not restricted to a specific "friend" implementation, feel free though to point out implementation specifics if relevant] Reading through the unanswered questions, I stumbled upon the InternalsVisibleTo…
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
14
votes
3 answers

how can i access internals in asp.net 5

Before asp.net 5 I would add "internalsVisibleTo(some.namespace.name)" to AssemblyInfo.cs - But I no longer have assemblyInfo.cs in my WebApi project. How do I expose internals in a WebAPI project to my unitTest project?
jenspo
  • 504
  • 4
  • 11
14
votes
3 answers

When should [assembly: InternalsVisibleTo()] be used?

I understand that the InternalVisibleTo attribute is used to expose types and methods with the internal access modifier to a specified assembly. I have only ever used this for exposing internal methods to a separate assembly containing a suite of…
fletcher
  • 13,380
  • 9
  • 52
  • 69
12
votes
4 answers

UnityContainer and internal constructor

I have a class with internal constructor and want to Resolve it from Unity (2.0). public class MyClass { internal MyClass(IService service) { } } then I'm doing _container.Resolve(); when I do so I have an exception Exception is:…
Shaddix
  • 5,901
  • 8
  • 45
  • 86
12
votes
2 answers

InternalsVisibleTo not working for Managed C++

InternalsVisibleTo is not working for my managed C++ projects, but it is for my C# projects. Any help would be appreciated. Here is a simplified layout. Project A - C#, has an internal property I want to access from B/C. Project B - Managed C++.…
Rob Prouse
  • 22,161
  • 4
  • 69
  • 89
10
votes
1 answer

How to mock/isolate internal classes in VS 2012 with Fakes Framework shims?

So, the issue is that I have a bunch of internal classes in my assembly that are used by the class I want to test. Since accessors have been removed from VS2012, I'm fine with using [InternalsVisibleTo] and that works great... except when I try to…
Finster
  • 499
  • 1
  • 7
  • 25
9
votes
1 answer

How can I make InternalsVisibleTo attribute work by signing a public token key safely?

I am trying to expose some internals to my unit test project by using: [assembly: InternalsVisibleTo("MyTest")] But I am getting the error: Error 1 Friend assembly reference MyTest' is invalid. Strong-name signed assemblies must specify a…
pencilCake
  • 51,323
  • 85
  • 226
  • 363
9
votes
2 answers

LinqPad access to internals of signed assemblies

Is there a signed version of LinqPad that I could use in order to access internals of signed assemblies?
Joao Silva
  • 876
  • 2
  • 8
  • 18
9
votes
1 answer

Can one use InternalVisibleTo without strong-named assembly in C#?

I could not find any clear statement on MSDN regarding this, there are some examples with strong names other are without, for me it seems like it should work even without but its not working. Thank you
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
8
votes
2 answers

InternalsVisibleTo with "private protected"

A new version of the .Net framework and C# offer a new access modifier: private protected. In order to access such a member, the class must both reside in the same assembly and derive from the defining class. (In contrast to protected internal…
Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33
8
votes
1 answer

Make specific internal function visible to another assembly (i.e. not all internals visible)

It's possible to specify [assembly: InternalsVisibleTo("NameOfOtherAssembly")] in AssemblyInfo.cs. But is it possible to restrict this to specific internal functions? Is there e.g. an attribute that's possible to apply to each…
David S.
  • 5,965
  • 2
  • 40
  • 77
7
votes
3 answers

Can I make internals visible to all assemblies signed with same key?

I have an assembly with functionality that I don't want exposed as public but still accessible to my other assemblies. This can be done using InternalsVisibleToAttribute by specifying each assembly that it will make its internals visible to. I was…
Luke Baulch
  • 3,626
  • 6
  • 36
  • 44
1
2 3 4 5