Files
ILSpy/ICSharpCode.BamlDecompiler/XmlnsDictionary.cs
T
Siegfried Pammer d8dfbec04b Write XAML a XAML parser can read back
Two of the defects reported on issue #2253 come from the decompiler
writing text that means something else when it is read again.

A markup extension is written as a single attribute value, and its
grammar gives ',' '=' '{' '}' and the quote characters a meaning. Values
went out unquoted, so an argument carrying any of them was read back as
further name/value pairs: {DXBinding Expr='Price - Prev > 0 ? ...'}, the
reported case, no longer compiles at all (MC3042, MC3045). Values without
such a character stay unquoted, because quoting them would rewrite every
document that never needed it.

A clr-namespace declaration names the CLR namespace it maps, but nothing
read that name out of it, so no lookup by namespace could match a
declaration the document itself had made. Every type in such a namespace
then got a second prefix declared on the element that used it. The
assembly is the second half of the same lookup, and there the document
records the name it was written against while a well-known type carries
the assembly it resolves to now - "mscorlib" against
"System.Private.CoreLib" - so the two are also accepted as the same when
the recorded assembly forwards the type.

Assisted-by: Claude:claude-opus-5:Claude Code
2026-09-04 19:40:59 +02:00

145 lines
3.7 KiB
C#

/*
Copyright (c) 2015 Ki
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.Collections.Generic;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.BamlDecompiler.Xaml;
namespace ICSharpCode.BamlDecompiler
{
internal class XmlnsScope : List<NamespaceMap>
{
public BamlElement Element { get; }
public XmlnsScope PreviousScope { get; }
public XmlnsScope(XmlnsScope prev, BamlElement elem)
{
PreviousScope = prev;
Element = elem;
}
public string LookupXmlns(string fullAssemblyName, string clrNs, string typeName = null)
{
foreach (var ns in this)
{
if (NamespaceMap.Matches(ns, fullAssemblyName, clrNs, typeName))
return ns.XMLNamespace;
}
return null;
}
}
internal class XmlnsDictionary
{
Dictionary<string, NamespaceMap> piMappings = new Dictionary<string, NamespaceMap>();
public XmlnsDictionary() => CurrentScope = null;
public XmlnsScope CurrentScope { get; set; }
public void PushScope(BamlElement element) => CurrentScope = new XmlnsScope(CurrentScope, element);
public void PopScope() => CurrentScope = CurrentScope.PreviousScope;
public void Add(NamespaceMap map) => CurrentScope.Add(map);
public void SetPIMapping(string xmlNs, string clrNs, string fullAssemblyName)
{
if (!piMappings.ContainsKey(xmlNs))
{
var map = new NamespaceMap(null, fullAssemblyName, xmlNs, clrNs);
piMappings[xmlNs] = map;
}
}
NamespaceMap PIFixup(NamespaceMap map)
{
if (piMappings.TryGetValue(map.XMLNamespace, out var piMap))
{
map.FullAssemblyName = piMap.FullAssemblyName;
map.CLRNamespace = piMap.CLRNamespace;
}
return map;
}
public NamespaceMap LookupNamespaceFromPrefix(string prefix)
{
var scope = CurrentScope;
while (scope != null)
{
foreach (var ns in scope)
{
if (ns.XmlnsPrefix == prefix)
return PIFixup(ns);
}
scope = scope.PreviousScope;
}
return null;
}
public NamespaceMap LookupNamespaceFromXmlns(string xmlNs)
{
var scope = CurrentScope;
while (scope != null)
{
foreach (var ns in scope)
{
if (ns.XMLNamespace == xmlNs)
return ns;
}
scope = scope.PreviousScope;
}
return null;
}
public string LookupXmlns(string fullAssemblyName, string clrNs, string typeName = null)
{
foreach (var map in piMappings)
{
if (NamespaceMap.Matches(map.Value, fullAssemblyName, clrNs, typeName))
return map.Key;
}
var scope = CurrentScope;
while (scope != null)
{
foreach (var ns in scope)
{
if (NamespaceMap.Matches(ns, fullAssemblyName, clrNs, typeName))
return ns.XMLNamespace;
}
scope = scope.PreviousScope;
}
return null;
}
}
}