首页 > 其他分享 >Declaring InternalsVisibleTo in the csproj

Declaring InternalsVisibleTo in the csproj

时间:2023-02-19 00:22:48浏览次数:49  
标签:MSBuild assembly csproj Declaring System project file InternalsVisibleTo

原文:Declaring InternalsVisibleTo in the csproj - Meziantou's blog

While I prefer testing the public API of an assembly, it's sometimes useful to test the implementation details. So, an attribute that I often use is [assembly: InternalsVisibleTo("MyAssembly.Tests")] to allow the test assembly to use internal classes or methods. A convention is to declare assembly attribute in the file AssemblyInfo.cs. With the new SDK-based project, there is no AssemblyInfo.cs by default to add assembly attributes. Of course, you can still create this file by your own, but why don't take advantage of the new SDK-based project to add this attribute. It's even possible to add it automatically to all your project!

If you remember the post I've written about Getting the date of build of a .NET assembly at runtime a few months ago where I also use the csproj file to add an assembly attribute. You can use the same way here:

csproj (MSBuild project file)
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
    <_Parameter1>ClassLibrary1.Tests</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>
</Project>

When compiling the project, a file will be generated by MSBuild in the obj folder:

C#
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

// 

标签:MSBuild,assembly,csproj,Declaring,System,project,file,InternalsVisibleTo
From: https://www.cnblogs.com/bruce1992/p/17134066.html

相关文章