From b4daeabcd906283d2845063bc681ca4fa69492e8 Mon Sep 17 00:00:00 2001 From: Shimmy Date: Tue, 19 Sep 2017 02:16:32 +0300 Subject: [PATCH] Data-annotations length fix (#24101) * Updated in MinLengthAttribute and MaxLengthAttribute to support ICollection * Added tests * Fixed typo * Trying to address two failing checks: - Linux x64 Release Build - UWP CoreCLR x64 Debug Build * Implemented changes requested in review - Extracted Count checking to an external helper to obey DRY - Removed dependency of ICollection and changed to simple reflection Count property lookup * Added requested tests * Added catch for MissingMetadataException. * Extracted code from try-catch. * Added comment as requested. * Typo correction --- .../DataAnnotations/MaxLengthAttribute.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/MaxLengthAttribute.cs b/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/MaxLengthAttribute.cs index 012b6ea01c97..06f4ba0ccb00 100644 --- a/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/MaxLengthAttribute.cs +++ b/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/MaxLengthAttribute.cs @@ -122,12 +122,24 @@ public static bool TryGetCount(object value, out int count) return true; } - PropertyInfo property = value.GetType().GetRuntimeProperty("Count"); + PropertyInfo property = null; + try + { + // On CoreRT, this property may not be enabled for reflection. + // It may be possible to eliminate the exception by using direct reflection + // (i.e. not via the RuntimeReflectionExtensions or the new split TypeInfo format. + property = value.GetType().GetRuntimeProperty("Count"); + } + catch (TypeAccessException) + { + } + if (property != null && property.CanRead && property.PropertyType == typeof(int)) { count = (int)property.GetValue(value); return true; } + count = -1; return false; }