On msdn, internal is explained as follows:
TheInternalKeyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly.
That is, only code call types or members in the same assembly are allowed.
Can I call these internal methods?
If the called Assembly uses internalsvisibletoattribute in the code to mark one or more friends assembly, the Assembly labeled as friends can access the internal method of the called assembly. the following example shows the code of Assembly A, which declares Assemblyb as a friend assembly.
// This file is for Assembly A.using System.Runtime.CompilerServices;using System;[assembly: InternalsVisibleTo("AssemblyB")]// The class is internal by default.class FriendClass{ public void Test() { Console.WriteLine("Sample Class"); }}// Public class that has an internal method.public class ClassWithFriendMethod{ internal void Test() { Console.WriteLine("Sample Method"); }}
An example of a specific line of code is as follows:
[assembly: InternalsVisibleTo("AssemblyB, PublicKey=32ab4ba45e0a69a1")]
So what if we want to call the internal method in the code written by a third party?
The answer is reflection.
The following is the source code of the called class.
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace internalclasstest{ public class PubClass { public void Speak() { Console.WriteLine("PubClass speaks: You are so nice!"); } //Internal method internal void Mock() { Console.WriteLine("PubClass mocks: You suck!"); } } //Internal class class InternalClass { public void Speak() { Console.WriteLine("InternalClass speaks: I love my job!"); } void Moci() { Console.WriteLine("InternalClass speaks: I love Friday night!"); } }}
The following is an example of code that uses mock to reflect and call the pubclass's internal function:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;namespace reflectionInternal{ class Program { static void Main(string[] args) { Assembly asm = Assembly.LoadFile(@"E:\internalclasstest\bin\Debug\internalclasstest.dll"); Type t1 = asm.GetType("internalclasstest.PubClass"); ConstructorInfo t1Constructor = t1.GetConstructor(Type.EmptyTypes); Object oPubClass = t1Constructor.Invoke(new Object[] { }); MethodInfo oMethod = t1.GetMethod("Mock", BindingFlags.Instance | BindingFlags.NonPublic); oMethod.Invoke(oPubClass, new Object[]{}); } }}
Source:
Internal (C # reference)
Http://msdn.microsoft.com/en-us/library/7c5ka91b%28VS.80%29.aspx
Friend assemblies (C # and Visual Basic)
Http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx
Accessing internal members via system. reflection?
Http://stackoverflow.com/questions/171332/accessing-internal-members-via-system-reflection
Using Reflection to call an internal Method
Http://www.codeproject.com/Messages/1635613/Using-reflection-to-call-an-internal-method.aspx
Methodbase. Invoke method (object, object [])
Http://msdn.microsoft.com/en-us/library/a89hcwhh.aspx
Bindingflags Enumeration
Http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx