public void ResolveNamespace(XElement elem, XamlContext ctx) { if (Namespace != null) return; // Since XmlnsProperty records are inside the element, // the namespace is resolved after processing the element body. string xmlNs = null; if (elem.Annotation<XmlnsScope>() != null) xmlNs = elem.Annotation<XmlnsScope>().LookupXmlns(Assembly, TypeNamespace); if (xmlNs == null) xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace); // Sometimes there's no reference to System.Xaml even if x:Type is used if (xmlNs == null) xmlNs = ctx.TryGetXmlNamespace(Assembly, TypeNamespace); if (xmlNs == null) { if (AssemblyNameComparer.CompareAll.Equals(Assembly, ctx.Module.Assembly)) xmlNs = $"clr-namespace:{TypeNamespace}"; else xmlNs = $"clr-namespace:{TypeNamespace};assembly={Assembly.Name}"; var nsSeg = TypeNamespace.Split('.'); var prefix = nsSeg[nsSeg.Length - 1].ToLowerInvariant(); if (string.IsNullOrEmpty(prefix)) { if (string.IsNullOrEmpty(TypeNamespace)) prefix = "global"; else prefix = "empty"; } int count = 0; var truePrefix = prefix; XNamespace prefixNs, ns = ctx.GetXmlNamespace(xmlNs); while ((prefixNs = elem.GetNamespaceOfPrefix(truePrefix)) != null && prefixNs != ns) { count++; truePrefix = prefix + count; } if (prefixNs == null) { elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(truePrefix), ns)); if (string.IsNullOrEmpty(TypeNamespace)) elem.AddBeforeSelf(new XComment(string.Format(dnSpy_BamlDecompiler_Resources.Msg_GlobalNamespace, truePrefix))); } } Namespace = ctx.GetXmlNamespace(xmlNs); }
public void ResolveNamespace(XElement elem, XamlContext ctx) { if (Namespace != null) { return; } // Since XmlnsProperty records are inside the element, // the namespace is resolved after processing the element body. string xmlNs = null; if (elem.Annotation <XmlnsScope>() != null) { xmlNs = elem.Annotation <XmlnsScope>().LookupXmlns(Assembly, TypeNamespace); } if (xmlNs == null) { xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace); } // Sometimes there's no reference to System.Xaml even if x:Type is used if (xmlNs == null) { xmlNs = ctx.TryGetXmlNamespace(Assembly, TypeNamespace); } if (xmlNs == null) { if (AssemblyNameComparer.CompareAll.Equals(Assembly, ctx.Module.Assembly)) { xmlNs = $"clr-namespace:{TypeNamespace}"; } else { xmlNs = $"clr-namespace:{TypeNamespace};assembly={Assembly.Name}"; } var nsSeg = TypeNamespace.Split('.'); var prefix = nsSeg[nsSeg.Length - 1].ToLowerInvariant(); if (string.IsNullOrEmpty(prefix)) { if (string.IsNullOrEmpty(TypeNamespace)) { prefix = "global"; } else { prefix = "empty"; } } int count = 0; var truePrefix = prefix; XNamespace prefixNs, ns = ctx.GetXmlNamespace(xmlNs); while ((prefixNs = elem.GetNamespaceOfPrefix(truePrefix)) != null && prefixNs != ns) { count++; truePrefix = prefix + count; } if (prefixNs == null) { elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(truePrefix), ns)); if (string.IsNullOrEmpty(TypeNamespace)) { elem.AddBeforeSelf(new XComment(string.Format(dnSpy_BamlDecompiler_Resources.Msg_GlobalNamespace, truePrefix))); } } } Namespace = ctx.GetXmlNamespace(xmlNs); }