Accessing Internal Method Outside of Assembly in C#
What is internal in C#?
It’s one of the keyword available in C# language. It means that, the members, classes and methods marked as internal are never exposed to other assembly.
In this post we would see how we expose to be available outside of current assembly.
Before doing anything, we should ask why. Well, in some advance scenarios, you might have to expose all internals to other assemblies. It might be a testing scenario, or it might be a change in architecture.
We would see how we achieve that.
Let’s take the following ClassLibrary project, which has a Person class.
The Person class has two methods, SayHello() method is public and SayHi() method is internal.
If we add reference of ClassLibrary1 to another project such as here ConsoleApplication1, we would be able to access the public method of Person class.
The following code displays that, it has access to SayHello() method and SayHi() method is never displayed.
To expose all the internals outside of current assembly, you need to navigate to the project and open the “AssemblyInfo.cs”, which resides in Properties folder.
Open the “AssemblyInfo.cs” file and add the InternalsVisibleTo method. Note that, you need to provide the target assembly name here.
Now rebuild the ClassLibrary1 project and you should see the SayHi() method in your ConsoleApplication1.
However, if you don’t want to expose all internals, you could individually select the internal key and change it to public. That would be tedious task, that’s the reason you could do achieve using this tip.
Hope this post was useful.
Thanks for reading. Like Method Of Tried in Facebook.