Reputation: 231
I'm completly new in MSBuild and I'm getting kinda confused with the so called metadata of MSBuild.
As I understand it the metadata in the documentation is equal to "a variable". But is there somehow a way to really embed custom metadata in the assembly through MSBuild?
<ItemGroup>
<Compile Include="one.cs" />
<Compile Include="two.cs">
<BuildDay>Tuesday</BuildDay>
</Compile>
</ItemGroup>
Upvotes: 0
Views: 534
Reputation: 100751
Metadata is used during the build process for various tasks. It isn't necessarily an input to the compiler or embedded into assemblies, but is mostly used by the targets and tasks that MSBuild and the compiler (Roslyn) ecosystem provide.
Custom metadata can be useful when extending the build with custom targets.
When you want to embed something into an assembly, you effectively need to generate code during the build and add items during the build. This would need to be done manually by writing msbuild targets.
An example where metadata is used to generate code is for the new "SDK-style" projects (default for .NET Core, .NET Standard and ASP.NET Core applications) where the assembly attributes that are usually in an AssemblyInfo.cs
file are generated during the build. There is an msbuild task called WriteCodeFragment
(code) that can read items and generate assembly attributes based on them wich is wired up by msbuld targets.
Using this to add additional attributes can then be done in the main project file by adding an item that this target and task understand:
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>My.Test.Project</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
Which then does the same thing as if I had manually added an AssemblyInfo.cs file and written the following code:
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("My.Test.Project")]
Upvotes: 2