Fixes 2307. MutableObjectBinder now relies on ModelMetadata to decide if a type is a complex type.

This avoids reflection at multiple places and allows caching.
This commit is contained in:
Harsh Gupta 2015-04-03 10:44:17 -07:00
parent 9330789fe3
commit 0c0fa10ed0
2 changed files with 16 additions and 6 deletions

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public virtual async Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext)
{
ModelBindingHelper.ValidateBindingContext(bindingContext);
if (!CanBindType(bindingContext.ModelType))
if (!CanBindType(bindingContext.ModelMetadata))
{
return null;
}
@ -188,20 +188,20 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
return false;
}
private static bool CanBindType(Type modelType)
private static bool CanBindType(ModelMetadata modelMetadata)
{
// Simple types cannot use this binder
var isComplexType = !TypeHelper.HasStringConverter(modelType);
if (!isComplexType)
if (!modelMetadata.IsComplexType)
{
return false;
}
if (modelType == typeof(ComplexModelDto))
if (modelMetadata.ModelType == typeof(ComplexModelDto))
{
// forbidden type - will cause a stack overflow if we try binding this type
return false;
}
return true;
}

View File

@ -13,6 +13,8 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </summary>
public abstract class ModelMetadata
{
private bool? _isComplexType;
/// <summary>
/// The default value of <see cref="ModelMetadata.Order"/>.
/// </summary>
@ -254,7 +256,15 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
/// </remarks>
public bool IsComplexType
{
get { return !TypeHelper.HasStringConverter(ModelType); }
get
{
if (_isComplexType == null)
{
_isComplexType = !TypeHelper.HasStringConverter(ModelType);
}
return _isComplexType.Value;
}
}
/// <summary>