Automatically Embed resources in dotnet

Friday, 26 November 2021
dotnet DirectoryBuildProps EmbeddedResource
Edit This Page

Automatically Embed Resources in dotnet

Scenario

I am working on source generators and I want to include my scriban template in the dll as an embedded resource. This makes for easier to read templates than using multiline strings in the C# code itself.

Solution

Inside your Directory.Build.props file add the following:

  <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">    
    <!-- Include each file with given extension from None collection to EmbeddedResource-->
    <ItemGroup>
      <EmbeddedResource Include="@(None -> WithMetadataValue('Extension', '.scriban'))" />
      <EmbeddedResource Include="@(None -> WithMetadataValue('Extension', '.MyCoolExtension'))" />
    </ItemGroup>
  </Target>

Validation

To validate your resources are included you can use Telerik's JustDecomplie

References

https://dominikjeske.github.io/source-generators/ https://stackoverflow.com/questions/51607558/msbuild-create-embeddedresource-before-build https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022 https://stackoverflow.com/questions/6631059/is-there-a-way-to-see-the-resources-that-are-in-a-net-dll