[assembly:InternalsVisibleToAttribute("MyFriendAssembly”)]Or (for a strong-named friend)
[assembly:InternalsVisibleToAttribute("MyFriendAssembly, PublicKeyToken=45cb56a45e0a69a1")]
This entry goes in "AssemblyInfo.cs" of the assembly whose internal classes are being exposed. The assembly mentioned in the attribute is the one that is being given permission to access those internal definitions.
For example if Assembly A is a business logic assembly and assembly B is a unit test assembly requiring access to the internals of assembly A then the attribute definition is placed inside assembly A mentioning assembly B inside the attribute so that it may have the required access.
This has changed from .NET 5.0
For example if the Unit Test project is called MyUnitTests.csproj and the target work project with internals methods and classes to expose; is called MyLibrary.csproj Then insert into MyLibrary.csproj project file the following lines:
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>MyUnitTests</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
Substitute Related Friend Error
I had some unit tests that failed because I was using Substitute to create a Substitute logger like so:MyClass myclass = new MyClass(Substitute.For<ILogger<CCTVCommands>>(), fileSystem);but then I got a horrible bug that looked like this:
Can not create proxy for type Microsoft.Extensions.Logging.ILogger`1[[MyClass, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because type MyClass is not accessible. Make it public, or internal and mark your assembly with [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2This was caused by the class being declared an internal class, to fix it I had to add:
<ItemGroup> <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute"> <_Parameter1>DynamicProxyGenAssembly2</_Parameter1> </AssemblyAttribute> </ItemGroup>to the project file of the project containing the internal class.
No comments:
Post a Comment