The access modifier internal in C # can be said to be between public and private.ProgramCentralized access is allowed. But sometimes there is such a requirement. We hope that the types in an assembly can be accessed by some external assemblies. In this case, it cannot be set to public. Otherwise, it can be accessed by all external assemblies. To meet the above requirements, we can use a friend assembly.
The following is a simple example to describe the user meta assembly.
1. Create two class library projects TESTA and testb in one solution, and create Class A and Class B respectively in the two class library projects.
2. Add a reference to project testb in project Testa.
3. Set the access level of Class B in project testb to internal.
NamespaceTestb {Internal classB{}}
4. Currently, Class B cannot be accessed in Class A of the testa project. If you want class A to be able to access Class B, you must add the assembly of Project Testa as the friend assembly of Project testb assembly. To add a friend Assembly, we need to use the internalisvisibleto feature. To use this feature, we need to add a namespace.
Using system. runtime. compilerservices.
5. Use the internalisvisibleto feature in Class B to add the Assembly Testa as a friend assembly.
UsingSystem;UsingSystem. Collections. Generic;UsingSystem. LINQ;UsingSystem. text;UsingSystem. runtime. compilerservices ;[Assembly:Internalsvisibleto("Testa")]NamespaceTestb {Internal classB{}}
6 The internalisvisibleto feature can also be added to assemblyinfo. CS.
7. Now you can access Class B of the testb project in Class A of Project Testa.