Search code examples
c#internal

Detect if the caller has access to my assembly internals


I need to detect in a method if its caller has internal access to my assembly. That is, if it is a) code in the same assembly or b) has a InternalsVisibleTo relationship with my assembly. Ideally the check should be fast.

What are my options?

UPDATE: I would also be partially satisfied if there was a good solution for part a of the problem.


Solution

  • Look into System.Diagnostics.StackTrace

        System.Diagnostics.StackTrace st = new StackTrace();
        MethodBase mb = st.GetFrame(1).GetMethod();
        Assembly a = mb.DeclaringType.Assembly;
    

    a) just compare current assembly to a

    b) i think you should be able to pick out the InternalsVisibleTo attributes on the assembly