You can work with .NET for two decades and still discover some useful stuff. One thing that bothered me till now is that an embedded resource name is “the project name” .“the path to the resource where each directory separator character is changed to a dot”. This is illustrated by the screenshot below:
The code to access the embedded resource at runtime looks like:
1 2 3 4 5 6 7 8 |
var defaultReportXsl = new XslCompiledTransform(); Assembly assembly = Assembly.GetExecutingAssembly(); using (var stream = assembly.GetManifestResourceStream(@"NetStandardAssembly.Dir1.NDependReport.xsl")) { Assert.IsNotNull(stream); using (XmlReader xr = XmlReader.Create(stream)) { defaultReportXsl.Load(xr); } } |
This is not nice because…
- if the path of the resource gets changed,
- or if the assembly name gets changed,
- or if the resource gets moved to another project
…in all these situations the resource name must be kept in-sync, else a nasty bug appear! However any developer task that requires something to be kept in-sync manually in more than one location is a highly error-prone task and necessarily end up badly.
Thanks to this stackoverflow answer I discovered that the XML element <LogicalName> can be used in the .csproj file to provide a unique name to the resource. Unfortunately in the resource Properties panel this logical name is not proposed (as shown by the screenshot above). Thus I wanted to share with the world this hidden-tip that works with all flavours of .NET (.NET Fx, .NET Standard; .NET Core, .NET 5/6…) 🙂