Skip to content

Commit 798b0c1

Browse files
author
Jonathan Peppers
committed
ApplyPropertiesVisitor - added a static cache for BindableProperty reflection
It seemed that Type.GetFields() was called ALOT
1 parent 743c1b3 commit 798b0c1

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class ApplyPropertiesVisitor : IXamlNodeVisitor
1919
XmlName.xFactoryMethod,
2020
XmlName.xName
2121
};
22+
static readonly ConcurrentDictionary<Type, Dictionary<string, FieldInfo>> _bindablePropertiesCache = new ConcurrentDictionary<Type, Dictionary<string, FieldInfo>>();
2223

2324
public ApplyPropertiesVisitor(HydratationContext context, bool stopOnResourceDictionary = false)
2425
{
@@ -273,8 +274,15 @@ static bool GetRealNameAndType(ref Type elementType, string namespaceURI, ref st
273274
static BindableProperty GetBindableProperty(Type elementType, string localName, IXmlLineInfo lineInfo,
274275
bool throwOnError = false)
275276
{
276-
var bindableFieldInfo =
277-
elementType.GetFields().FirstOrDefault(fi => fi.Name == localName + "Property" && fi.IsStatic && fi.IsPublic);
277+
Dictionary<string, FieldInfo> fields;
278+
if (!_bindablePropertiesCache.TryGetValue(elementType, out fields))
279+
{
280+
_bindablePropertiesCache[elementType] =
281+
fields = elementType.GetFields().Where(fi => fi.Name.EndsWith("Property", StringComparison.Ordinal) && fi.IsStatic && fi.IsPublic).ToDictionary(fi => fi.Name);
282+
}
283+
284+
FieldInfo bindableFieldInfo;
285+
fields.TryGetValue(localName + "Property", out bindableFieldInfo);
278286

279287
Exception exception = null;
280288
if (exception == null && bindableFieldInfo == null)

0 commit comments

Comments
 (0)