From a04f5cba9ab36ecd01d2616f13dcda2bdeed8514 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 28 Sep 2016 23:23:17 +0100 Subject: [PATCH] Remove ViewDataEvaluator.GetPropertyValue Closure (#5325) - part of #3918 --- .../ViewFeatures/ViewDataEvaluator.cs | 2 +- .../ViewFeatures/ViewDataInfo.cs | 40 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataEvaluator.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataEvaluator.cs index ed3ccbe49a..e52b336458 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataEvaluator.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataEvaluator.cs @@ -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 diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataInfo.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataInfo.cs index c43eaa68c4..62e273a4ae 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataInfo.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/ViewDataInfo.cs @@ -8,6 +8,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures { public class ViewDataInfo { + private static readonly Func _propertyInfoResolver = () => null; + private object _value; private Func _valueAccessor; @@ -15,6 +17,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures /// Initializes a new instance of the class with info about a /// lookup which has already been evaluated. /// + /// The that was evaluated from. + /// The evaluated value. public ViewDataInfo(object container, object value) { Container = container; @@ -23,8 +27,25 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures /// /// Initializes a new instance of the class with info about a - /// lookup which is evaluated when is read. + /// lookup which is evaluated when is read. + /// It uses on + /// passing parameter to lazily evaluate the value. /// + /// The that will be evaluated from. + /// The that will be used to evalute . + public ViewDataInfo(object container, PropertyInfo propertyInfo) + : this(container, propertyInfo, _propertyInfoResolver) + { + } + + /// + /// Initializes a new instance of the class with info about a + /// lookup which is evaluated when is read. + /// It uses to lazily evaluate the value. + /// + /// The that has the . + /// The that represents 's property. + /// public ViewDataInfo(object container, PropertyInfo propertyInfo, Func 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; + } } }