// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Reflection; namespace Microsoft.AspNetCore.Mvc.ViewFeatures { public class ViewDataInfo { private static readonly Func _propertyInfoResolver = () => null; private object _value; private Func _valueAccessor; /// /// 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; _value = value; } /// /// Initializes a new instance of the class with info about a /// 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 evaluate . 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. /// A delegate that will return the . public ViewDataInfo(object container, PropertyInfo propertyInfo, Func valueAccessor) { Container = container; PropertyInfo = propertyInfo; _valueAccessor = valueAccessor; } public object Container { get; } public PropertyInfo PropertyInfo { get; } public object Value { get { if (_valueAccessor != null) { ResolveValue(); } return _value; } set { _value = value; _valueAccessor = null; } } private void ResolveValue() { if (ReferenceEquals(_valueAccessor, _propertyInfoResolver)) { _value = PropertyInfo.GetValue(Container); } else { _value = _valueAccessor(); } _valueAccessor = null; } } }