From bff069a9e269f08f243df9b0ea7539f29f071f0d Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 4 Sep 2026 11:36:25 +0200 Subject: [PATCH] Put a XAML document where its code-behind is The project exporter wrote every XAML document to the project root under a fully-qualified name while the code-behind class went into a directory named after its namespace, so the two halves of one partial class ended up in different places. WPF tooling pairs MainWindow.xaml with MainWindow.xaml.cs by name and location; anything else is an unrelated file to it, and --nested-directories made the split wider still by moving only the C# half. Both now go through one function that decides where a type's files live, so the document lands where the type's own C# file would have, and the code-behind is named after the document. The BAML writers of the UI and of the command line had grown their own copies of the naming, which is how they came to disagree with the C# writer in the first place. Assisted-by: Claude:claude-opus-5:Claude Code --- .../WholeProjectDecompiler.cs | 53 +++++++++++++----- ICSharpCode.Decompiler/PartialTypeInfo.cs | 9 +++ .../BamlFixtureTypes.cs | 28 +++++++++ .../ProjectExportBamlTests.cs | 26 +++++++++ .../fixtures/test.g.resources | Bin 289 -> 465 bytes .../BamlAwareWholeProjectDecompiler.cs | 6 +- ILSpy/TreeNodes/BamlResourceNodeFactory.cs | 11 ++-- 7 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 ICSharpCode.ILSpyCmd.Tests/BamlFixtureTypes.cs diff --git a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs index 5cb703218..542774548 100644 --- a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs +++ b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/WholeProjectDecompiler.cs @@ -387,23 +387,26 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler string GetFileFileNameForHandle(TypeDefinitionHandle h) { - var type = metadata.GetTypeDefinition(h); - string file = CleanUpFileName(metadata.GetString(type.Name), ".cs"); - string ns = metadata.GetString(type.Namespace); - if (string.IsNullOrEmpty(ns)) - { - return file; - } - else + // A code-behind class belongs to the document it completes: WPF tooling expects + // MainWindow.xaml.cs beside MainWindow.xaml, and treats a stray MainWindow.cs + // elsewhere in the tree as an unrelated file. + foreach (var partialType in partialTypes) { - string dir = Settings.UseNestedDirectoriesForNamespaces ? CleanUpPath(ns) : CleanUpDirectoryName(ns); - if (directories.Add(dir)) + if (partialType.DeclaringTypeDefinitionHandle == h && partialType.CompanionFileName != null) { - var path = Path.Combine(TargetDirectory, dir); - CreateDirectory(path); + string companionDirectory = Path.GetDirectoryName(partialType.CompanionFileName)!; + if (!string.IsNullOrEmpty(companionDirectory) && directories.Add(companionDirectory)) + CreateDirectory(Path.Combine(TargetDirectory, companionDirectory)); + return partialType.CompanionFileName + ".cs"; } - return Path.Combine(dir, file); } + + var type = metadata.GetTypeDefinition(h); + string fileName = GetFileNameForType(metadata.GetString(type.Namespace), metadata.GetString(type.Name), ".cs"); + string directory = Path.GetDirectoryName(fileName)!; + if (!string.IsNullOrEmpty(directory) && directories.Add(directory)) + CreateDirectory(Path.Combine(TargetDirectory, directory)); + return fileName; } void ProcessFiles(List> files) @@ -972,6 +975,30 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler /// Removes invalid characters from file names and reduces their length, /// but keeps file extensions and path structure intact. /// + /// + /// The path of a file belonging to a type: the namespace becomes directories or one + /// flattened directory name, depending on + /// . Everything a type + /// owns - its C# file and the XAML document it is the code-behind of - goes here, so the + /// two end up next to each other. + /// + public static string GetFileNameForType(string @namespace, string typeName, string extension, + bool useNestedDirectoriesForNamespaces) + { + string file = CleanUpFileName(typeName, extension); + if (string.IsNullOrEmpty(@namespace)) + return file; + string directory = useNestedDirectoriesForNamespaces + ? CleanUpPath(@namespace) + : CleanUpDirectoryName(@namespace); + return Path.Combine(directory, file); + } + + protected string GetFileNameForType(string @namespace, string typeName, string extension) + { + return GetFileNameForType(@namespace, typeName, extension, Settings.UseNestedDirectoriesForNamespaces); + } + public static string SanitizeFileName(string fileName) { return CleanUpName(fileName, separateAtDots: false, treatAsFileName: true, treatAsPath: true); diff --git a/ICSharpCode.Decompiler/PartialTypeInfo.cs b/ICSharpCode.Decompiler/PartialTypeInfo.cs index 27564d4fd..27775e2d3 100644 --- a/ICSharpCode.Decompiler/PartialTypeInfo.cs +++ b/ICSharpCode.Decompiler/PartialTypeInfo.cs @@ -16,6 +16,8 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +#nullable enable + using System; using System.Collections.Generic; using System.Diagnostics; @@ -43,6 +45,13 @@ namespace ICSharpCode.Decompiler public TypeDefinitionHandle DeclaringTypeDefinitionHandle { get; } + /// + /// The document this type is the code-behind of, as a project-relative path + /// ("Views/MainWindow.xaml"), where there is one. The project decompiler names the type's + /// C# file after it, so that the two sit next to each other the way the tooling expects. + /// + public string? CompanionFileName { get; set; } + public void AddDeclaredMember(IMember member) { declaredMembers.Add(member.MetadataToken); diff --git a/ICSharpCode.ILSpyCmd.Tests/BamlFixtureTypes.cs b/ICSharpCode.ILSpyCmd.Tests/BamlFixtureTypes.cs new file mode 100644 index 000000000..859114e59 --- /dev/null +++ b/ICSharpCode.ILSpyCmd.Tests/BamlFixtureTypes.cs @@ -0,0 +1,28 @@ +// Copyright (c) 2026 Siegfried Pammer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +namespace ICSharpCode.ILSpyCmd.Tests.Views +{ + /// + /// Plays the role of a WPF code-behind class: the BAML fixture "views/deeppage.baml" names it + /// as its root, which is how the BAML decompiler recognises an x:Class type. + /// + public class DeepPage + { + } +} diff --git a/ICSharpCode.ILSpyCmd.Tests/ProjectExportBamlTests.cs b/ICSharpCode.ILSpyCmd.Tests/ProjectExportBamlTests.cs index 08f1a886b..9449ef325 100644 --- a/ICSharpCode.ILSpyCmd.Tests/ProjectExportBamlTests.cs +++ b/ICSharpCode.ILSpyCmd.Tests/ProjectExportBamlTests.cs @@ -80,6 +80,32 @@ namespace ICSharpCode.ILSpyCmd.Tests }); } + [Test] + public async Task TheCodeBehindSitsNextToItsDocument() + { + // WPF tooling pairs MainWindow.xaml with MainWindow.xaml.cs by name and location; a + // code-behind anywhere else is an unrelated file as far as the project is concerned. + await RunAsync(testAssemblyPath, "--disable-updatecheck", "-p", "-o", outputDirectory); + + string documentDirectory = Path.Combine(outputDirectory, "ICSharpCode.ILSpyCmd.Tests.Views"); + Assert.Multiple(() => { + Assert.That(File.Exists(Path.Combine(documentDirectory, "DeepPage.xaml")), Is.True, "the document"); + Assert.That(File.Exists(Path.Combine(documentDirectory, "DeepPage.xaml.cs")), Is.True, "its code-behind"); + }); + } + + [Test] + public async Task DocumentsFollowTheNamespaceDirectoriesToo() + { + await RunAsync(testAssemblyPath, "--disable-updatecheck", "-p", "--nested-directories", "-o", outputDirectory); + + string documentDirectory = Path.Combine(outputDirectory, "ICSharpCode", "ILSpyCmd", "Tests", "Views"); + Assert.Multiple(() => { + Assert.That(File.Exists(Path.Combine(documentDirectory, "DeepPage.xaml")), Is.True, "the document"); + Assert.That(File.Exists(Path.Combine(documentDirectory, "DeepPage.xaml.cs")), Is.True, "its code-behind"); + }); + } + [Test] public async Task TheOldOptInFlagStillWorks() { diff --git a/ICSharpCode.ILSpyCmd.Tests/fixtures/test.g.resources b/ICSharpCode.ILSpyCmd.Tests/fixtures/test.g.resources index aadddbfe29fb6f749d7c41dd094292ff1fe26123..51c8b22a96811ba53c405fb56d29fd1b0f01da3d 100644 GIT binary patch delta 179 zcmZ3;bdh<&LPaJ92ncX=0iplv9&hG5)}*Zr6&7NgxJy+-t&Aa)A(f$=p_oCRAqB`v zWhel`M22)AOOGK5$jW8NVQ`vw$CXL3V&X%!P#HM}1}RVH;Ecqg0_XgcR6S3h;DSo$ o+!Vc#)Z&t2S$&ZCnz$9{g=MCe7wfsCrWOPwrl&FkRkJVw06haRMF0Q* delta 39 mcmcb}ypU