5

I am confused by some code that shouldn't be working, but oddly enough, is working and I know I'm simply overlooking something obvious. I'm looking at the source code for the Accord.NET framework and I downloaded it and its compiling just fine, but I'm confused about something. In one of the assemblies, called Accord.Math is a file called Indices.cs. Here is the definition:

internal static class Indices
{
    // Lots of code
    // ...
    // ...
}

You can see this on line 35.

Over in another assembly, called Accord.Statistics, there is a file called Tools.cs. In that file, there is this line:

return Accord.Math.Indices.Random(k, n);

You can see this on line 329.

I am confused on how this line can reference the Accord.Math.Indices class since it is marked as internal. My understanding is that a class marked as internal can only be accessed by classes that reside in the same DLL file. Can someone explain how this is working?

Icemanind
  • 47,519
  • 50
  • 171
  • 296

1 Answers1

7

This is because in file AssemblyInfo.cs you have these attributes:

[assembly: InternalsVisibleTo("Accord.Tests.Math, PublicKey=...")] 
[assembly: InternalsVisibleTo("Accord.Tests.MachineLearning,...")] 
[assembly: InternalsVisibleTo("Accord.Tests.Statistics,...")] 
[assembly: InternalsVisibleTo("Accord.Statistics, ...")]

These attributes specify that types that are ordinarily visible only within the current assembly are visible to a specified assembly (in the case that you asked it is visible to Accord.Statistics).

You can read more about InternalsVisibleToAttribute on MSDN

dotnetom
  • 24,551
  • 9
  • 51
  • 54
  • Just when I thought I knew every trick in the C# book....I had no idea you could do this. Thank you for answering this! – Icemanind Aug 05 '15 at 05:33
  • I followed your links, but still have no idea how to implement this. How do you actually reference from your .cs project the, let's say, .dll that contains all those internal classes? It mentions friends in the documentation, but there are no mentions of path or how to link them together. – m3.b Jan 11 '22 at 04:22