'How do I fix the error "Can not access internal class" error in C#?
I am pretty much using existing code to create a new class in a new file. I am using existing code to create a test class. One of the declarations does not compile and gives me a red warning in Resharper stating:
Cannot access internal class "..." here.
I am using the namespace where the class is defined. What else am I missing?
Solution 1:[1]
You should mark the class as public instead of internal.
Even if you use the appropriate namespace, it is reasonable why you get an error like this. The reason is that you expose an internal class through a public class, which is not allowed. A class that is internal can be accessed only by types in the assembly that the class is defined or by assemblies where the internal classes of an assembly can be viewed (please have a look at InternalsVisibleToAttribute).
Solution 2:[2]
If you are using .NET 5+, you can add this setting to your .csproj file to make internals of your app visible to another assembly (for instance to a test project):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="ProjName.Tests" />
</ItemGroup>
</Project>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | VahidN |
