Search code examples
c#.netasp.net-core.net-core.net-4.7

Is it possible to add packages for "InternalsVisibleTo" at the command line or via a config file?


We have some methods that are internal but that people may need visible in test projects (and only test projects). To enable that, we had to add a bunch of InternalsVisibleTo statements at the top of a class. This is a library that may be used by many other projects and to help with testing and verification, we'd like to make the packages that can see the internals be settable at runtime.

Is this possible?

Example (Pseudo code):

[assembly: InternalsVisibleTo( System.ReadFromCommandLine("package"))]

Solution

  • No, it's not possible. Attributes like InternalsVisibleTo must have their values be compile time constants.

    An expression E is an attribute_argument_expression if all of the following statements are true:

    • The type of E is an attribute parameter type (Attribute parameter types).
    • At compile-time, the value of E can be resolved to one of the following:
      • A constant value.
      • A System.Type object.
      • A one-dimensional array of attribute_argument_expressions.

    Boldface added, source.