Giving file in the project a name A.Designer.cs will make it nested to A.cs

49 Views Asked by At

I just discovered an interesting feature of Visual Studio (using VS2022), renaming existing class or creating new with same name as another class plus ".Designer" will make it nested to another class.

To example for a simple console project:

enter image description here

Nothing in csproj-file indicates it is nested. I would normally expect something like

<ItemGroup>
  <Compile Update="Localization\LocalizedStrings.cs">
    <DesignTime>True</DesignTime>
    <AutoGen>True</AutoGen>
    <DependentUpon>LocalizedStrings.tt</DependentUpon>
  </Compile>
  <Compile Update="Views\SoftwareView.axaml.cs">
    <DependentUpon>SoftwareView.axaml</DependentUpon>
  </Compile>
   ...

I found this answer explaining different roles of designer files. It seems they are expected to be autogenerated and treated specially.

Earlier I was using addon.

Question: is it safe to use this "trick" to make something nested? I hope nothing will get overwritten? Are there more tricks related to auto-nesting files by name or something else?

1

There are 1 best solutions below

0
Sinatr On

I'd say it was

Bad idea

Certain features are not working inside such ".Designer.cs" file.

  1. Some (all?) warnings are not shown (e.g. C8618) for classes inside this file. The compilier errors are shown and stopping build as normal though.

  2. Code Cleanup doesn't perform anything: doesn't format, doesn't remove unused using, etc. But Intellisense support is fully working or at least I didn't noticed any difference.

That's already bad enough.

VS is treating such files differently, assuming they are indeed auto-generated by some tools and leaving only bare minimum of checking. Not designed to be edited by human.