-1

Okay, I have a solution I am working on that has 4 different projects in it. One of my projects (a console application) is trying to make reference to some of the classes defined in another project (a library); the only problem is, those called are defined as Internal in the library.

How can I use these Internal classes in other assemblies/projects in the same solution? I added references to the library, but that did not help. It is saying that the protection level is too high (because it is internal, it is only available in that assembly).

Musicode
  • 661
  • 1
  • 12
  • 29
  • 1
    Why would you do that? Either change the qualifier to public or accept that internals are not visible outside. Or use reflection. – Wiktor Zychla Nov 02 '13 at 22:25
  • possible duplicate of [c# Instantiating Internal class with private constructor](http://stackoverflow.com/questions/2023193/c-sharp-instantiating-internal-class-with-private-constructor) – Michael J. Gray Nov 02 '13 at 22:26
  • 1
    You could use this to make the internal classes accessible in a friendly assembly http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx – Dan Nov 02 '13 at 22:28
  • Because I have a Class Library as one project, and applications as the other. Should I use my Interface to access the Class Library? I think I am confused on concepts here. – Musicode Nov 02 '13 at 22:31
  • @Wiktor: For example if someone is writing a framework and does not want to expose the internal parts to the applications using the framework but wants to structure the project using different assemblies. – Dan Nov 02 '13 at 22:33
  • @DNA_Instant: `internal` *means* "This should not be accessible from other projects". If you want to access it from outside the project, declare it `public`. – Heinzi Nov 02 '13 at 22:38
  • Hi all again, I actually solved this problem in a way different than recommended. I created a public Interface which "corresponds" with the internal library, by creating objects from that library with data specified. – Musicode Nov 02 '13 at 22:51
  • As an example: I couldn't just create a "Node" object because it was an internal class only, so instead, I make an instance of an interface INodeFactory. INodeFactory is an interface to a class in the internal library which has the ability to create a Node. – Musicode Nov 02 '13 at 22:52

2 Answers2

1

You generally shouldn't access something that's internal. That defeats the purpose of declaring it so in the first place. If, however, you do need to...

If you can change the assembly with internal things, either:

  1. Make the classes public, or
  2. Use the InternalsVisibleToAttribute to expose it to just the assemblies you want to.

If you cannot change it, or decide not to, then you can use reflection to access the internal classes. For some portions of what you then do with the class, you should be able to use the dynamic keyword to make access easier and faster than with reflection.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

You must use reflection to access the internal classes in other assemblies, but it will be significantly slower and not generally optimizable by the compiler. It's also somewhat complicated.

It is recommended that you expose a public class that wraps your internal classes and methods from the other assembly, or simply switch the internal classes in the other assembly over to public.

Michael J. Gray
  • 9,784
  • 6
  • 38
  • 67