InternalsVisibleTo attribute isn't working

后端 未结 19 2490
眼角桃花
眼角桃花 2020-11-27 13:51

I am trying to use the InternalsVisibleTo assembly attribute to make my internal classes in a .NET class library visible to my unit test project. For some reas

19条回答
  •  再見小時候
    2020-11-27 13:57

    Another possibility that may be tricky to track down, depending on how your code is written.

    1. You're invoking an internal method defined in X from another assembly Y
    2. The method signature uses internal types defined in Z
    3. You then have to add [InternalsVisibleTo] in X AND in Z

    For example:

    // In X
    internal static class XType
    {
        internal static ZType GetZ() { ... }
    }
    
    // In Y:
    object someUntypedValue = XType.GetZ();
    
    // In Z:
    internal class ZType { ... }
    

    If you have it written like above, where you're not referring to ZType directly in Y, after having added Y as a friend of X, you may be mystified why your code still doesn't compile.

    The compilation error could definitely be more helpful in this case.

提交回复
热议问题