Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xaml] improve performance in debug-mode #21460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions src/Controls/src/Xaml/ApplyPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,26 +326,15 @@ static bool GetRealNameAndType(ref Type elementType, string namespaceURI, ref st
return false;
}

static BindableProperty GetBindableProperty(Type elementType, string localName, IXmlLineInfo lineInfo,
bool throwOnError = false)
static BindableProperty GetBindableProperty(Type elementType, string localName, IXmlLineInfo lineInfo)
{
// F# does not support public fields, so allow internal (Assembly) as well as public
const BindingFlags supportedFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
var bindableFieldInfo = elementType.GetFields(supportedFlags)
.FirstOrDefault(fi => (fi.IsAssembly || fi.IsPublic) && fi.Name == localName + "Property");

Exception exception = null;
if (exception == null && bindableFieldInfo == null)
var bindableFieldInfo = elementType.GetField(localName + "Property", supportedFlags);
if (bindableFieldInfo is not null && (bindableFieldInfo.IsAssembly || bindableFieldInfo.IsPublic))
{
exception =
new XamlParseException(
Format("BindableProperty {0} not found on {1}", localName + "Property", elementType.Name), lineInfo);
}

if (exception == null)
return bindableFieldInfo.GetValue(null) as BindableProperty;
if (throwOnError)
throw exception;
}
return null;
}

Expand All @@ -355,7 +344,7 @@ static object GetTargetProperty(object xamlelement, XmlName propertyName, object
//If it's an attached BP, update elementType and propertyName
var bpOwnerType = xamlelement.GetType();
GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, rootElement, lineInfo);
var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
var property = GetBindableProperty(bpOwnerType, localName, lineInfo);

if (property != null)
return property;
Expand Down Expand Up @@ -397,7 +386,7 @@ void registerSourceInfo(object target, string path)
//If it's an attached BP, update elementType and propertyName
var bpOwnerType = element.GetType();
var attached = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, rootElement, lineInfo);
var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
var property = GetBindableProperty(bpOwnerType, localName, lineInfo);

//If the target is an event, connect
if (xpe == null && TryConnectEvent(element, localName, attached, value, rootElement, lineInfo, out xpe))
Expand Down Expand Up @@ -452,7 +441,7 @@ public static object GetPropertyValue(object xamlElement, XmlName propertyName,
//If it's an attached BP, update elementType and propertyName
var bpOwnerType = xamlElement.GetType();
var attached = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, rootElement, lineInfo);
var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false);
var property = GetBindableProperty(bpOwnerType, localName, lineInfo);

//If it's a BindableProberty, GetValue
if (xpe == null && TryGetValue(xamlElement, property, attached, out var value, lineInfo, out xpe, out targetProperty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static void AssertWarnings(this List<WarningsPerFile> actualWarnings, Lis
Code = "IL2070",
Messages = new List<string>
{
"Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetBindableProperty(Type,String,IXmlLineInfo,Boolean): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetFields(BindingFlags)'. The parameter '#0' of method 'Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetBindableProperty(Type,String,IXmlLineInfo,Boolean)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.",
"Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetBindableProperty(Type,String,IXmlLineInfo): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.NonPublicFields' in call to 'System.Type.GetField(String,BindingFlags)'. The parameter '#0' of method 'Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetBindableProperty(Type,String,IXmlLineInfo)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.",
"Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetAllRuntimeMethods(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The parameter '#0' of method 'Microsoft.Maui.Controls.Xaml.ApplyPropertiesVisitor.GetAllRuntimeMethods(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.",
}
},
Expand Down
Loading