From 9893605d98490c853da5ff695ef019726cf65f14 Mon Sep 17 00:00:00 2001 From: mattbrailsford Date: Thu, 28 Jul 2016 19:09:40 +0100 Subject: [PATCH] Fixes issue #181 by checking to see if an own property matches some minimum requirements before mapping --- .../Processors/UmbracoPropertyAttribute.cs | 2 +- .../Internal/PropertyInfoExtensions.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertyAttribute.cs b/src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertyAttribute.cs index 9f57f11..67aeaa4 100644 --- a/src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertyAttribute.cs +++ b/src/Our.Umbraco.Ditto/ComponentModel/Processors/UmbracoPropertyAttribute.cs @@ -118,7 +118,7 @@ public override object ProcessValue() { var contentProperty = contentType.GetProperty(umbracoPropertyName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Static); - if (contentProperty != null) + if (contentProperty != null && contentProperty.IsMappable()) { // This is more than 2x as fast as propertyValue = contentProperty.GetValue(content, null); propertyValue = PropertyInfoInvocations.GetValue(contentProperty, content); diff --git a/src/Our.Umbraco.Ditto/Extensions/Internal/PropertyInfoExtensions.cs b/src/Our.Umbraco.Ditto/Extensions/Internal/PropertyInfoExtensions.cs index 68460be..96fbec8 100644 --- a/src/Our.Umbraco.Ditto/Extensions/Internal/PropertyInfoExtensions.cs +++ b/src/Our.Umbraco.Ditto/Extensions/Internal/PropertyInfoExtensions.cs @@ -36,5 +36,27 @@ public static bool IsVirtualAndOverridable(this PropertyInfo source) var method = source.GetGetMethod(); return method.IsVirtual && !method.IsFinal; } + + /// + /// Checks to see if a model property is mappable by Ditto + /// + /// + /// The source . + /// + /// + /// True if the is mappable; otherwise, false. + /// + public static bool IsMappable(this PropertyInfo source) + { + // Make sure source is readable + if (!source.CanRead) return false; + + // Check to make sure the get method has no parameters + var hasParams = source.GetIndexParameters().GetLength(0) > 0; + if (hasParams) return false; + + // All checks have passed so allow it to be mapped + return true; + } } } \ No newline at end of file