Remove ViewDataEvaluator.GetPropertyValue Closure (#5325)

- part of #3918
This commit is contained in:
Ben Adams 2016-09-28 23:23:17 +01:00 committed by Doug Bunting
parent 0a4c06eefb
commit a04f5cba9a
2 changed files with 38 additions and 4 deletions

View File

@ -203,7 +203,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
return null;
}
return new ViewDataInfo(container, propertyInfo, () => propertyInfo.GetValue(container));
return new ViewDataInfo(container, propertyInfo);
}
private struct ExpressionPair

View File

@ -8,6 +8,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
public class ViewDataInfo
{
private static readonly Func<object> _propertyInfoResolver = () => null;
private object _value;
private Func<object> _valueAccessor;
@ -15,6 +17,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
/// Initializes a new instance of the <see cref="ViewDataInfo"/> class with info about a
/// <see cref="ViewDataDictionary"/> lookup which has already been evaluated.
/// </summary>
/// <param name="container">The <see cref="object"/> that <paramref name="value"/> was evaluated from.</param>
/// <param name="value">The evaluated value.</param>
public ViewDataInfo(object container, object value)
{
Container = container;
@ -23,8 +27,25 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
/// <summary>
/// Initializes a new instance of the <see cref="ViewDataInfo"/> class with info about a
/// <see cref="ViewDataDictionary"/> lookup which is evaluated when <see cref="Value"/> is read.
/// <see cref="ViewDataDictionary"/> lookup which is evaluated when <see cref="Value"/> is read.
/// It uses <see cref="System.Reflection.PropertyInfo.GetValue(object)"/> on <paramref name="propertyInfo"/>
/// passing parameter <paramref name="container"/> to lazily evaluate the value.
/// </summary>
/// <param name="container">The <see cref="object"/> that <see cref="Value"/> will be evaluated from.</param>
/// <param name="propertyInfo">The <see cref="PropertyInfo"/> that will be used to evalute <see cref="Value"/>.</param>
public ViewDataInfo(object container, PropertyInfo propertyInfo)
: this(container, propertyInfo, _propertyInfoResolver)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ViewDataInfo"/> class with info about a
/// <see cref="ViewDataDictionary"/> lookup which is evaluated when <see cref="Value"/> is read.
/// It uses <paramref name="valueAccessor"/> to lazily evaluate the value.
/// </summary>
/// <param name="container">The <see cref="object"/> that has the <see cref="Value"/>.</param>
/// <param name="propertyInfo">The <see cref="PropertyInfo"/> that represents <see cref="Value"/>'s property.</param>
/// <param name="valueAccessor"></param>
public ViewDataInfo(object container, PropertyInfo propertyInfo, Func<object> valueAccessor)
{
Container = container;
@ -42,8 +63,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
if (_valueAccessor != null)
{
_value = _valueAccessor();
_valueAccessor = null;
ResolveValue();
}
return _value;
@ -54,5 +74,19 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
_valueAccessor = null;
}
}
private void ResolveValue()
{
if (ReferenceEquals(_valueAccessor, _propertyInfoResolver))
{
_value = PropertyInfo.GetValue(Container);
}
else
{
_value = _valueAccessor();
}
_valueAccessor = null;
}
}
}