When nesting a .resx
file under a .cs
file using the DependentUpon
element in the .csproj
file and then accessing a resource using the wrapper a MissingManifestResourceException
is thrown.
The message reads:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Namespace.Resource.resources" was correctly embedded or linked into assembly "Assembly" at compile time, or that all the satellite assemblies required are loadable and fully signed.
Example code:
MessageBox.Show(Resource.Key);
Why does this error occur?
The MSBuild target
PrepareResourceNames
(on whichPrepareResources
, which also callsResGen
, depends on) calls a task calledCreateManifestResourceName
.[see
Microsoft.Common.targets
]This task creates the name of the
.resources
file. For C# the actual method that creates the name isCreateManifestNameImpl
inMicrosoft.Build.Tasks.CreateCSharpManifestResourceName
. This method checks whether the.resx
file has a.cs
file as parent. In that case it searches the.cs
file for the first fully qualified class name it can find and takes it as the.resources
file name.[see
Microsoft.Build.Tasks.v4.0.dll
]The
.resx
wrapper does not expect this file name change and thus an exception is thrown.I guess this behavior was implemented for WinForms localization which nests its resources under the corresponding form or control.
I decided not to nest the
.resx
files. A possible solution could be to implement aCreateCustomManifestResourceName
task for which MSBuild provides a hook.