NDepend

Improve your .NET code quality with NDepend

How to Logically Name Embedded Resources in .csproj?

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:

Embedded-Resources-Naming

The code to access the embedded resource at runtime looks like:

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…)  🙂

Embedded-Resources-Naming-Logical-Name

My dad being an early programmer in the 70's, I have been fortunate to switch from playing with Lego, to program my own micro-games, when I was still a kid. Since then I never stop programming.

I graduated in Mathematics and Software engineering. After a decade of C++ programming and consultancy, I got interested in the brand new .NET platform in 2002. I had the chance to write the best-seller book (in French) on .NET and C#, published by O'Reilly and also did manage some academic and professional courses on the platform and C#.

Over my consulting years I built an expertise about the architecture, the evolution and the maintenance challenges of large & complex real-world applications. It seemed like the spaghetti & entangled monolithic legacy concerned every sufficiently large team. As a consequence, I got interested in static code analysis and started the project NDepend in 2004.

Nowadays NDepend is a full-fledged Independent Software Vendor (ISV). With more than 12.000 client companies, including many of the Fortune 500 ones, NDepend offers deeper insight and full control on their application to a wide range of professional users around the world.

I live with my wife and our twin kids Léna and Paul in the beautiful island of Mauritius in the Indian Ocean.