Skip to content

Commit

Permalink
Data-annotations length fix (dotnet#24101)
Browse files Browse the repository at this point in the history
* Updated in MinLengthAttribute and MaxLengthAttribute to support ICollection<T>

* 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<T> 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
  • Loading branch information
weitzhandler authored and danmoseley committed Sep 18, 2017
1 parent bafbafd commit b4daeab
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit b4daeab

Please sign in to comment.