/// <summary> [EXPERIMENTAL] Gets the specified property. When lazy initialization occurs, the specified initialization function is used.
        /// </summary>
        /// <typeparam name="TRet">The type of the return value.</typeparam>
        /// <param name="propertyExpression">The property expression.</param>
        /// <param name="valueFactory">The delegate that is invoked to produce the lazily initialized value when it is needed.</param>
        /// <returns>The value</returns>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException"></exception>
        public TRet GetValue <TRet>([NotNull] Expression <Func <TRet> > propertyExpression, [NotNull] Func <TRet> valueFactory)
        {
            if (valueFactory == null)
            {
                throw new ArgumentNullException(nameof(valueFactory));
            }
            var name = MemberNameUtil.GetPropertyName(propertyExpression);

            if (!_fields.ContainsKey(name))
            {
                if (_readOnlyCollection)
                {
                    throw new KeyNotFoundException();
                }
                var value = valueFactory();
                AddCore(name, value);
                if (Equals(value, default(TRet)))
                {
                    OnPropertyChanged(name, default(TRet), value);
                }
            }
            return((TRet)_fields[name].Value);
        }
Пример #2
0
        /// <summary> Example: this.ℝ(x => x.Parent.Parent)
        /// </summary>
        /// <typeparam name="TIn"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        /// <param name="v"></param>
        /// <param name="f"></param>
        /// <returns></returns>
        public static TOut ℝ <TIn, TOut>(this TIn v, Expression <Func <TIn, TOut> > f) where TIn : class  where TOut : class
        {
            var path = MemberNameUtil.SplitPropertyPath(f).ToList();
            var o    = (object)v;

            path.RemoveAt(0);
            while (path.Count > 0)
            {
                if (o == null)
                {
                    return(null);
                }
                var m            = path[0]; path.RemoveAt(0);
                var propertyInfo = o.GetType().GetProperty(m);
                o = propertyInfo.GetValue(o, null);
            }
            return((TOut)o);

//			var compiledExpression = f.Compile();
//			var result = compiledExpression(v);
//
//			return result;
        }
 public IBackingFieldInfo this[Expression <Func <object> > memberExpression] => _fields[MemberNameUtil.GetPropertyName(memberExpression)];
 public void Add <T>(Expression <Func <T> > memberExpression, T value = default(T))
 {
     Add(MemberNameUtil.GetPropertyName(memberExpression), value);
 }