| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The interface '<InterfaceName>' cannot be implemented in this project. It can only be implemented in '<Assembly>' or in a project to which '<Assembly>' grants access with the InternalsVisibleTo attribute. |
Cause
Some interfaces are public so that you can consume them, but they aren't meant to be implemented outside of the project that declares them. Such interfaces are marked with the [InternalImplement] attribute. Most interfaces of Metalama, for instance those of the code model like IDeclaration or IMethod, are marked this way: Metalama provides their implementation, and it can add members to them in any version.
An analyzer included in the Metalama.Framework package reports LAMA0120 when a class, struct, or interface of your project implements such an interface, directly or indirectly through another interface, unless one of the following conditions is true:
- The interface is declared in the current project.
- The project that declares the interface grants access to the current project with the [InternalsVisibleTo] attribute.
The <InterfaceName> placeholder is the interface, and <Assembly> is the assembly that declares it.
This diagnostic is reported by a Roslyn analyzer, which doesn't run in the snapshot tests of this documentation, so the article has no example.
Resolution
- Don't implement the interface. To give a type the behavior you need, find a factory or an extension method in the API that creates or wraps the object. For instance, use TypeFactory to get IType objects instead of implementing IType.
- If you need an object with a similar shape for your own code, for instance for testing, define your own interface or class instead of implementing the Metalama interface.
- If the interface belongs to one of your own projects, either move the implementing type to that project or add an
[assembly: InternalsVisibleTo( "YourProject" )]attribute to the project that declares the interface.