Skip to content

Commit

Permalink
ApplyPropertiesVisitor - added a static cache for BindableProperty re…
Browse files Browse the repository at this point in the history
…flection

It seemed that Type.GetFields() was called ALOT
  • Loading branch information
Jonathan Peppers committed Jun 29, 2016
1 parent 743c1b3 commit 798b0c1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Xamarin.Forms.Xaml/ApplyPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class ApplyPropertiesVisitor : IXamlNodeVisitor
XmlName.xFactoryMethod,
XmlName.xName
};
static readonly ConcurrentDictionary<Type, Dictionary<string, FieldInfo>> _bindablePropertiesCache = new ConcurrentDictionary<Type, Dictionary<string, FieldInfo>>();

public ApplyPropertiesVisitor(HydratationContext context, bool stopOnResourceDictionary = false)
{
Expand Down Expand Up @@ -273,8 +274,15 @@ static bool GetRealNameAndType(ref Type elementType, string namespaceURI, ref st
static BindableProperty GetBindableProperty(Type elementType, string localName, IXmlLineInfo lineInfo,
bool throwOnError = false)
{
var bindableFieldInfo =
elementType.GetFields().FirstOrDefault(fi => fi.Name == localName + "Property" && fi.IsStatic && fi.IsPublic);
Dictionary<string, FieldInfo> fields;
if (!_bindablePropertiesCache.TryGetValue(elementType, out fields))
{
_bindablePropertiesCache[elementType] =
fields = elementType.GetFields().Where(fi => fi.Name.EndsWith("Property", StringComparison.Ordinal) && fi.IsStatic && fi.IsPublic).ToDictionary(fi => fi.Name);
}

FieldInfo bindableFieldInfo;
fields.TryGetValue(localName + "Property", out bindableFieldInfo);

Exception exception = null;
if (exception == null && bindableFieldInfo == null)
Expand Down

0 comments on commit 798b0c1

Please sign in to comment.