Alsalam alikom wa r7amat Allah wa barakatoh (Peace upon you..)
Long story short, you have AssemblyA (with source code) and AssemblyB… you want AssemblyB to be able to access internal members (types, data, methods.. etc) of AssemblyA…
You can of course do that through Reflection but come on, it’s not a descent way to do it.. right??
.Net framework offers an attribute called InternalsVisibleToAttribute [MSDN Link]. It works on .NET 2.0 and above. An example grabbed from MSDN:
This way, you can easily use Class1 and Class2.Test from AssemblyB code (obviously after adding reference to AssemblyA).
I had 2 questions in mind after reading this,
- This can be some sort of a hack, I simply create another asesmbly that replaces your AssemblyB and violla, I’ve access to your internal members…
A: You can include the public token key for AssemblyB in InternalsVisibleTo constructor
- Can I selectively determine what classes (types) are visible to AssemblyB?
.NET offers a class called StrongNameIdentityPermission [MSDN Link] Which can solve both of these problems actually.
I’ll not go into details for how to do it, but let me state what can you do using this class..
You can require that calling assemblies (e.g AssemblyB) must be strongly signed using a private key you provide.. (Imagine you are building a multi-tier application and you don’t want anybody else to just call your public classes…)
StrongNameIdentityPermission works only on public classes, while InternalsVisibleToAttribute works on assembly level.
One last thing, it’s actually kind of funny.. InternalsVisibleTo constructor has 2 overloads (only in .NET3.5), the second accepted named parameter named AllIntenalsVisible (bool) which gave me hope that I can somehow selectively choose what to make visible… but… here is what I found in MSDN Documentation:
This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Catch you later…