mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
An assembly can map one CLR namespace to several XML namespaces: PresentationFramework maps its namespaces to both the winfx/2006 and the netfx/2007 presentation namespace. The fallback used when the BAML xmlns records name no namespace for a type always preferred the winfx/2006 one, so a document that binds the default prefix to netfx/2007 ended up with a root start tag that declares one presentation namespace and needs the other for its own name, which XmlWriter rejects. Only the root carries the xmlns declaration, which is why skipping XClassRewritePass worked around it. Assisted-by: Claude:claude-opus-5:Claude Codepull/4096/head
8 changed files with 199 additions and 2 deletions
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
<ContextMenu x:Class="ILSpy.BamlDecompiler.Tests.Cases.Issue1688" xmlns="http://schemas.microsoft.com/netfx/2007/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cases="clr-namespace:ILSpy.BamlDecompiler.Tests.Cases"> |
||||
<MenuItem Header="Assign Place" Click="Click_AssignPlace" /> |
||||
<MenuItem Header="Assign Move" Click="Click_AssignMove" /> |
||||
</ContextMenu> |
||||
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
// 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.
|
||||
|
||||
using System.Windows; |
||||
using System.Windows.Controls; |
||||
|
||||
namespace ILSpy.BamlDecompiler.Tests.Cases |
||||
{ |
||||
/// <summary>
|
||||
/// Interaction logic for Issue1688.xaml
|
||||
/// </summary>
|
||||
public partial class Issue1688 : ContextMenu |
||||
{ |
||||
public Issue1688() |
||||
{ |
||||
InitializeComponent(); |
||||
} |
||||
|
||||
void Click_AssignPlace(object sender, RoutedEventArgs e) |
||||
{ |
||||
} |
||||
|
||||
void Click_AssignMove(object sender, RoutedEventArgs e) |
||||
{ |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,115 @@
@@ -0,0 +1,115 @@
|
||||
// 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.
|
||||
|
||||
using System; |
||||
using System.IO; |
||||
using System.Reflection.PortableExecutable; |
||||
using System.Xml.Linq; |
||||
|
||||
using ICSharpCode.BamlDecompiler; |
||||
using ICSharpCode.Decompiler.Metadata; |
||||
using ICSharpCode.Decompiler.TypeSystem; |
||||
|
||||
using NUnit.Framework; |
||||
|
||||
using ILSpy.BamlDecompiler.Tests; |
||||
|
||||
// This assembly plays the role of an assembly that maps one CLR namespace to two XML namespaces,
|
||||
// the way PresentationFramework maps its namespaces to both presentation namespaces.
|
||||
[assembly: System.Windows.Markup.XmlnsDefinition(XmlNamespaceResolutionTests.Winfx2006Presentation, XmlNamespaceResolutionTests.TestClrNamespace)] |
||||
[assembly: System.Windows.Markup.XmlnsDefinition(XmlNamespaceResolutionTests.Netfx2007Presentation, XmlNamespaceResolutionTests.TestClrNamespace)] |
||||
|
||||
namespace System.Windows.Markup |
||||
{ |
||||
/// <summary>
|
||||
/// Stand-in for the WPF attribute of the same name, which is unavailable on platforms without
|
||||
/// WPF. The BAML decompiler matches it by full name in metadata, so the declaring assembly does
|
||||
/// not matter.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] |
||||
internal sealed class XmlnsDefinitionAttribute : Attribute |
||||
{ |
||||
public XmlnsDefinitionAttribute(string xmlNamespace, string clrNamespace) |
||||
{ |
||||
XmlNamespace = xmlNamespace; |
||||
ClrNamespace = clrNamespace; |
||||
} |
||||
|
||||
public string XmlNamespace { get; } |
||||
public string ClrNamespace { get; } |
||||
} |
||||
} |
||||
|
||||
namespace ILSpy.BamlDecompiler.Tests |
||||
{ |
||||
/// <summary>
|
||||
/// Tests for the fallback used when neither the BAML xmlns records nor the PI mappings name an
|
||||
/// XML namespace for a type: which of the assembly's XmlnsDefinition mappings is picked.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class XmlNamespaceResolutionTests |
||||
{ |
||||
public const string Winfx2006Presentation = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; |
||||
public const string Netfx2007Presentation = "http://schemas.microsoft.com/netfx/2007/xaml/presentation"; |
||||
public const string TestClrNamespace = "ILSpy.BamlDecompiler.Tests"; |
||||
|
||||
static IModule GetTestAssemblyModule() |
||||
{ |
||||
var location = typeof(XmlNamespaceResolutionTests).Assembly.Location; |
||||
using var stream = new FileStream(location, FileMode.Open, FileAccess.Read); |
||||
var file = new PEFile(location, stream, streamOptions: PEStreamOptions.PrefetchEntireImage); |
||||
var resolver = new UniversalAssemblyResolver(location, throwOnError: false, |
||||
file.DetectTargetFrameworkId(), file.DetectRuntimePack()); |
||||
return new BamlDecompilerTypeSystem(file, resolver).MainModule; |
||||
} |
||||
|
||||
[Test] |
||||
public void PrefersPresentationNamespace_WhenDocumentDeclaresNothing() |
||||
{ |
||||
var xmlNs = XamlContext.TryGetXmlNamespace(GetTestAssemblyModule(), TestClrNamespace); |
||||
|
||||
Assert.That(xmlNs, Is.EqualTo(Winfx2006Presentation)); |
||||
} |
||||
|
||||
[Test] |
||||
public void PrefersNamespaceDeclaredByDocument_OverPresentationNamespace() |
||||
{ |
||||
// Issue #1688: the document binds the default prefix to the netfx/2007 presentation
|
||||
// namespace. Resolving its elements to the winfx/2006 one made the root start tag both
|
||||
// declare and redefine the default prefix, which XmlWriter rejects.
|
||||
var root = new XElement(XName.Get("Root", Netfx2007Presentation), |
||||
new XAttribute("xmlns", Netfx2007Presentation)); |
||||
|
||||
var xmlNs = XamlContext.TryGetXmlNamespace(GetTestAssemblyModule(), TestClrNamespace, root); |
||||
|
||||
Assert.That(xmlNs, Is.EqualTo(Netfx2007Presentation)); |
||||
} |
||||
|
||||
[Test] |
||||
public void PrefersNamespaceDeclaredByAncestor() |
||||
{ |
||||
var child = new XElement(XName.Get("Child", Netfx2007Presentation)); |
||||
new XElement(XName.Get("Root", Netfx2007Presentation), |
||||
new XAttribute("xmlns", Netfx2007Presentation), child); |
||||
|
||||
var xmlNs = XamlContext.TryGetXmlNamespace(GetTestAssemblyModule(), TestClrNamespace, child); |
||||
|
||||
Assert.That(xmlNs, Is.EqualTo(Netfx2007Presentation)); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue