2

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"))]
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Either you should make those methods public, or you should stub/mock them inside the tests of those other projects, is my opinion. – Jesse de Wit Jul 23 '19 at 14:43
  • There should be a one-to-one correlation between a library and a test project for that library. Additionally, regardless of *where* you add `InternalsVisibleTo`, the entire contents of the library are made visible to the specified project, so adding this on top of every class makes no sense, i.e. you're not just exposing this one particular class. `InternalsVisibleTo` is an assembly directive. Long and short, you should only need to add this once, and there should really only be one project you add it for. – Chris Pratt Jul 23 '19 at 18:10

1 Answers1

2

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.

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34