Home > Mobile >  How do I prevent nested C# classes with .resx files using the same named .resources file?
How do I prevent nested C# classes with .resx files using the same named .resources file?

Time:01-15

When generating a .resources file for a C# class which has a .resx file, Visual Studio 2022 only uses the namespace and outer class name to generate the name of the .resources file. If there is more than one class (with a .resx file) nested in the same outer class, the names of the .resources file will be same, resulting in error MSB3577.

Is there anyway to resolve this? For example, is there a parameter in the entry in the .csproj file to set the output name? Or do I just avoid using nested classes when .resx files are involved?

Background

Why do I have nested Form classes?

There is a C# control with the class name SpecialTrackBar. This class accepts a collection of DataItem objects, so there are DataItem and DataItemCollection classes. The dialog for editing the collection is DataItemCollectionEditorDialog.

I didn't want to put these classes at the same namespace as SpecialTrackBar as they may conflict with other control classes, and I didn't want to make a super long class name like SpecialTrackBarDataItemCollectionEditorDialog.

Using nested classes and having SpecialTrackBar.DataItemCollectionEditorDialog seemed like a good compromise. I'm hoping there is an alternative naming solution.

Steps to replicate:

  1. In Visual Studio 2022, create a new Windows Forms Control Library using .NET Framework 4.6.2 named "NestedResTest".

  2. Add two Windows Forms, Dialog1 and Dialog2. The files Dialog1.cs, Dialog1.Designer.cs, and Dialog1.resx and the corresponding files for Dialog2 will be automatically created. The Dialog1 and Dialog2 classes will both be in the namespace NestedResTest.

  3. Set the MSBuild project build output verbosity to "Diagnostic" (Tools -> Options -> Projects and Solutions -> Build And Run).

  4. Build NestedResTest. The build should succeed. Search the Build output for the line with "OutputResources=" and you should see two .resources files: NestedResTest.Dialog1.resources NestedResTest.Dialog2.resources

  5. Edit Dialog1.cs and Dialog1.Designer.cs and put the Dialog1 class inside the partial class InnerClass.

  6. Build NestedResTest. The build should succeed. Search the Build output for the line with "OutputResources=" and you should see two .resources files: NestedResTest.InnerClass.resources NestedResTest.Dialog2.resources

  7. Edit Dialog2.cs and Dialog2.Designer.cs and put the Dialog2 class inside the partial class InnerClass.

  8. Build NestedResTest. The build should FAIL with error MSB3577. Search the Build output for the line with "OutputResources=" and you should see two .resources files, both with the same name: NestedResTest.InnerClass.resources

CodePudding user response:

I assume you know main consequence of nesting the Form classes is loosing designer support for designing the form.

That said, if you want to fix the resource name issues, you can control how the resource manifest files named using either of the following options:

  • Specify <ManifestResourceName>
  • Or specify <LogicalName>
  • Or remove <DependentUpon>

Example - Control how manifest resource names generate

  1. Right click one the project and Unload the project

  2. Find the <EmbeddedResource> that you are interested in and do one of the following:

    Specify <ManifestResourceName>

    <EmbeddedResource Include="Dialog1.resx">
        <ManifestResourceName>RealProxyExample.Dialog1</ManifestResourceName>
        <DependentUpon>Dialog1.cs</DependentUpon>
    </EmbeddedResource>
    

    OR Specify <LogicalName>

    <EmbeddedResource Include="Dialog1.resx">
        <LogicalName>RealProxyExample.Dialog1.resources</LogicalName>
        <DependentUpon>Dialog1.cs</DependentUpon>
    </EmbeddedResource>
    

    OR Remove <DependentUpon>

    <EmbeddedResource Include="Dialog1.resx">
    </EmbeddedResource>
    
  3. Right click on the project and Reload.

Build and run the project. You will see the resources have been applied and are working as expected.

  • Related