示例#1
0
        /// <summary>
        /// Creates a new property rule from a lambda expression.
        /// </summary>
        public static PropertyRule Create <T, TProperty>(Expression <Func <T, TProperty> > expression, Func <CascadeMode> cascadeModeThunk)
        {
            var member = expression.GetMember();
            // We can't use the expression tree as a key in the cache, as it doesn't implement GetHashCode/Equals in a useful way.
            // Instead we'll use the MemberInfo as the key, but this only works for member expressions.
            // If this is not a member expression (eg, a RuleFor(x => x) or a RuleFor(x => x.Foo())) then we won't cache the result.
            // We could probably make the cache more robust in future.
            var compiled = member == null || ValidatorOptions.DisableAccessorCache ? expression.Compile() : AccessorCache <T> .GetCachedAccessor(member, expression);

            return(new PropertyRule(member, compiled.CoerceToNonGeneric(), expression, cascadeModeThunk, typeof(TProperty), typeof(T)));
        }