[X] don't expant types to Extension for x:Static

if a type name exists with an Extension suffix, xaml rules require us to
use that. But not for x:Static

- fixes #11833
This commit is contained in:
Stephane Delcroix 2023-09-08 16:35:26 +02:00
Родитель 5180d5789c
Коммит ecc18d1ce9
18 изменённых файлов: 36 добавлений и 34 удалений

Просмотреть файл

@ -24,7 +24,7 @@ namespace Microsoft.Maui.Controls.Build.Tasks
var typename = member.Substring(0, dotIdx);
var membername = member.Substring(dotIdx + 1);
var typeRef = module.ImportReference(XmlTypeExtensions.GetTypeReference(context.Cache, typename, module, node as BaseNode));
var typeRef = module.ImportReference(XmlTypeExtensions.GetTypeReference(context.Cache, typename, module, node as BaseNode, expandToExtension: false));
var fieldRef = GetFieldReference(context.Cache, typeRef, membername, module);
var propertyDef = GetPropertyDefinition(context.Cache, typeRef, membername, module);

Просмотреть файл

@ -190,7 +190,7 @@ namespace Microsoft.Maui.Controls.Build.Tasks
//The order of lookup is to look for the Extension-suffixed class name first and then look for the class name without the Extension suffix.
XmlType type = new XmlType(namespaceuri, name + "Extension", typeArguments);
if (!type.TryGetTypeReference(contextProvider.Context.Cache, contextProvider.Context.Module, null, out _))
if (!type.TryGetTypeReference(contextProvider.Context.Cache, contextProvider.Context.Module, null, expandToExtension: true, out _))
type = new XmlType(namespaceuri, name, typeArguments);
if (type == null)

Просмотреть файл

@ -50,12 +50,12 @@ namespace Microsoft.Maui.Controls.Build.Tasks
return xmlnsDefinitions;
}
public static TypeReference GetTypeReference(XamlCache cache, string typeName, ModuleDefinition module, BaseNode node)
public static TypeReference GetTypeReference(XamlCache cache, string typeName, ModuleDefinition module, BaseNode node, bool expandToExtension = true)
{
try
{
XmlType xmlType = TypeArgumentsParser.ParseSingle(typeName, node.NamespaceResolver, (IXmlLineInfo)node);
return GetTypeReference(xmlType, cache, module, node as IXmlLineInfo);
return GetTypeReference(xmlType, cache, module, node as IXmlLineInfo, expandToExtension: expandToExtension);
}
catch (XamlParseException)
{
@ -63,12 +63,12 @@ namespace Microsoft.Maui.Controls.Build.Tasks
}
}
public static TypeReference GetTypeReference(XamlCache cache, string namespaceURI, string typename, ModuleDefinition module, IXmlLineInfo xmlInfo)
public static TypeReference GetTypeReference(XamlCache cache, string namespaceURI, string typename, ModuleDefinition module, IXmlLineInfo xmlInfo, bool expandToExtension = true)
{
return new XmlType(namespaceURI, typename, null).GetTypeReference(cache, module, xmlInfo);
}
public static bool TryGetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo, out TypeReference typeReference)
public static bool TryGetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo, bool expandToExtension, out TypeReference typeReference)
{
IList<XmlnsDefinitionAttribute> xmlnsDefinitions = cache.GetXmlsDefinitions(module, GatherXmlnsDefinitionAttributes);
@ -81,7 +81,7 @@ namespace Microsoft.Maui.Controls.Build.Tasks
if (type is not null && type.IsPublicOrVisibleInternal(module))
return type;
return null;
});
}, expandToExtension: expandToExtension);
if (type != null && typeArguments != null && type.HasGenericParameters)
type = module.ImportReference(type).MakeGenericInstanceType(typeArguments.Select(x => x.GetTypeReference(cache, module, xmlInfo)).ToArray());
@ -89,9 +89,9 @@ namespace Microsoft.Maui.Controls.Build.Tasks
return (typeReference = (type == null) ? null : module.ImportReference(type)) != null;
}
public static TypeReference GetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo)
public static TypeReference GetTypeReference(this XmlType xmlType, XamlCache cache, ModuleDefinition module, IXmlLineInfo xmlInfo, bool expandToExtension = true)
{
if (TryGetTypeReference(xmlType, cache, module, xmlInfo, out TypeReference typeReference))
if (TryGetTypeReference(xmlType, cache, module, xmlInfo, expandToExtension: expandToExtension, out TypeReference typeReference))
return typeReference;
throw new BuildException(BuildExceptionCode.TypeResolution, xmlInfo, null, $"{xmlType.NamespaceUri}:{xmlType.Name}");

Просмотреть файл

@ -5,7 +5,7 @@ namespace Microsoft.Maui.Controls.Xaml
{
public interface IXamlTypeResolver
{
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider = null);
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider = null, bool expandToExtension = true);
bool TryResolve(string qualifiedTypeName, out Type type);
}
}

Просмотреть файл

@ -1888,7 +1888,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -1847,7 +1847,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -1847,7 +1847,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -1666,7 +1666,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -1736,7 +1736,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -1662,7 +1662,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -1662,7 +1662,7 @@
~Microsoft.Maui.Controls.Xaml.IReferenceProvider.FindByName(string name) -> object
~Microsoft.Maui.Controls.Xaml.IRootObjectProvider.RootObject.get -> object
~Microsoft.Maui.Controls.Xaml.IValueProvider.ProvideValue(System.IServiceProvider serviceProvider) -> object
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.Resolve(string qualifiedTypeName, System.IServiceProvider serviceProvider = null, bool expandToExtension = true) -> System.Type
~Microsoft.Maui.Controls.Xaml.IXamlTypeResolver.TryResolve(string qualifiedTypeName, out System.Type type) -> bool
~Microsoft.Maui.Controls.Xaml.IXmlLineInfoProvider.XmlLineInfo.get -> System.Xml.IXmlLineInfo
~Microsoft.Maui.Controls.Xaml.XamlParseException.XamlParseException(string message, System.Exception innerException) -> void

Просмотреть файл

@ -306,7 +306,7 @@ namespace Microsoft.Maui.Controls.Xaml
localname = localname.Substring(dotIdx + 1);
XamlParseException xpe;
elementType = XamlParser.GetElementType(new XmlType(namespaceURI, typename, null), lineInfo,
rootElement.GetType().Assembly, out xpe);
rootElement.GetType().Assembly, true, out xpe);
if (xpe != null)
throw xpe;

Просмотреть файл

@ -48,7 +48,7 @@ namespace Microsoft.Maui.Controls.Xaml
{
object value = null;
var type = XamlParser.GetElementType(node.XmlType, node, Context.RootElement?.GetType().Assembly,
var type = XamlParser.GetElementType(node.XmlType, node, Context.RootElement?.GetType().Assembly, true,
out XamlParseException xpe);
if (xpe != null)
{

Просмотреть файл

@ -26,7 +26,7 @@ namespace Microsoft.Maui.Controls.Xaml
var typename = Member.Substring(0, dotIdx);
var membername = Member.Substring(dotIdx + 1);
var type = typeResolver.Resolve(typename, serviceProvider);
var type = typeResolver.Resolve(typename, serviceProvider, expandToExtension: false);
var pinfo = type.GetRuntimeProperties().FirstOrDefault(pi => pi.Name == membername && pi.GetMethod.IsStatic);
if (pinfo != null)

Просмотреть файл

@ -361,7 +361,7 @@ namespace Microsoft.Maui.Controls.Xaml
#if !NETSTANDARD
[RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)]
#endif
public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, bool expandToExtension,
out XamlParseException exception)
{
bool hasRetriedNsSearch = false;
@ -379,7 +379,8 @@ namespace Microsoft.Maui.Controls.Xaml
if (t is not null && t.IsPublicOrVisibleInternal(currentAssembly))
return t;
return null;
});
},
expandToExtension);
var typeArguments = xmlType.TypeArguments;
exception = null;
@ -403,7 +404,7 @@ namespace Microsoft.Maui.Controls.Xaml
XamlParseException innerexception = null;
var args = typeArguments.Select(delegate (XmlType xmltype)
{
var t = GetElementType(xmltype, xmlInfo, currentAssembly, out XamlParseException xpe);
var t = GetElementType(xmltype, xmlInfo, currentAssembly, true, out XamlParseException xpe);
if (xpe != null)
{
innerexception = xpe;

Просмотреть файл

@ -207,9 +207,9 @@ namespace Microsoft.Maui.Controls.Xaml.Internals
this.getTypeFromXmlName = getTypeFromXmlName ?? throw new ArgumentNullException();
}
Type IXamlTypeResolver.Resolve(string qualifiedTypeName, IServiceProvider serviceProvider)
Type IXamlTypeResolver.Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, bool expandToExtension)
{
var type = Resolve(qualifiedTypeName, serviceProvider, out XamlParseException e);
var type = Resolve(qualifiedTypeName, serviceProvider, expandToExtension: expandToExtension, out XamlParseException e);
if (e != null)
throw e;
return type;
@ -217,18 +217,18 @@ namespace Microsoft.Maui.Controls.Xaml.Internals
bool IXamlTypeResolver.TryResolve(string qualifiedTypeName, out Type type)
{
type = Resolve(qualifiedTypeName, null, out XamlParseException exception);
type = Resolve(qualifiedTypeName, null, true, out XamlParseException exception);
return exception == null;
}
internal bool TryResolve(XmlType xmlType, out Type type)
{
XamlParseException exception;
type = getTypeFromXmlName(xmlType, null, currentAssembly, out exception);
type = getTypeFromXmlName(xmlType, null, currentAssembly, true, out exception);
return exception == null;
}
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out XamlParseException exception)
Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, bool expandToExtension, out XamlParseException exception)
{
IXmlLineInfo xmlLineInfo = null;
if (serviceProvider != null)
@ -238,10 +238,10 @@ namespace Microsoft.Maui.Controls.Xaml.Internals
}
var xmlType = TypeArgumentsParser.ParseSingle(qualifiedTypeName, namespaceResolver, xmlLineInfo);
return getTypeFromXmlName(xmlType, xmlLineInfo, currentAssembly, out exception);
return getTypeFromXmlName(xmlType, xmlLineInfo, currentAssembly, expandToExtension, out exception);
}
internal delegate Type GetTypeFromXmlName(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, out XamlParseException exception);
internal delegate Type GetTypeFromXmlName(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, bool expandToExtension, out XamlParseException exception);
}
class XamlRootObjectProvider : IRootObjectProvider
@ -330,7 +330,7 @@ namespace Microsoft.Maui.Controls.Xaml.Internals
&& node.Parent is IElementNode parent
&& XamlParser.GetElementType(parent.XmlType,
new XmlLineInfo(((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition),
context.RootElement.GetType().Assembly, out var xpe) is Type parentType
context.RootElement.GetType().Assembly, true, out var xpe) is Type parentType
&& parentType.GetRuntimeProperties().FirstOrDefault(p => p.Name == name.LocalName) is PropertyInfo propertyInfo
&& propertyInfo.PropertyType == typeof(BindingBase))
{

Просмотреть файл

@ -37,7 +37,8 @@ namespace Microsoft.Maui.Controls.Xaml
this XmlType xmlType,
IEnumerable<XmlnsDefinitionAttribute> xmlnsDefinitions,
string defaultAssemblyName,
Func<(string typeName, string clrNamespace, string assemblyName), T> refFromTypeInfo)
Func<(string typeName, string clrNamespace, string assemblyName), T> refFromTypeInfo,
bool expandToExtension = true)
where T : class
{
var lookupAssemblies = new List<XmlnsDefinitionAttribute>();
@ -61,7 +62,7 @@ namespace Microsoft.Maui.Controls.Xaml
}
var lookupNames = new List<string>(capacity: 2);
if (elementName != "DataTemplate" && !elementName.EndsWith("Extension", StringComparison.Ordinal))
if (expandToExtension && elementName != "DataTemplate" && !elementName.EndsWith("Extension", StringComparison.Ordinal))
lookupNames.Add(elementName + "Extension");
lookupNames.Add(elementName);

Просмотреть файл

@ -792,7 +792,7 @@ namespace Microsoft.Maui.Controls.Xaml.UnitTests
public void BindingIsResolvedAsBindingExtension()
// https://github.com/xamarin/Microsoft.Maui.Controls/issues/3606#issuecomment-422377338
{
var bindingType = XamlParser.GetElementType(new XmlType("http://schemas.microsoft.com/dotnet/2021/maui", "Binding", null), null, null, out var ex);
var bindingType = XamlParser.GetElementType(new XmlType("http://schemas.microsoft.com/dotnet/2021/maui", "Binding", null), null, null, true, out var ex);
Assert.That(ex, Is.Null);
Assert.That(bindingType, Is.EqualTo(typeof(BindingExtension)));
var module = ModuleDefinition.CreateModule("foo", new ModuleParameters()