コード例 #1
0
        public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (!target.HasValue || !value.HasValue)
            {
                return(Defer(target, value));
            }

            if (target.Value == null)
            {
                return(BinderHelper.NullTargetResult(target, errorSuggestion));
            }
            if (value.Value == null)
            {
                return(BinderHelper.SetNullValue(this, value));
            }


            var memberAccess = _memberResolver.MemberExpression(Name, target, MemberAccessMode.Write);

            var restrictions = BinderHelper.CreateCommonRestrictions(target);

            if (memberAccess == null || !memberAccess.Type.IsAssignableFrom(value.RuntimeType))
            {
                BindingEventSource.Log.SetMemberResolutionFailure(Name, target.RuntimeType.FullName, value.RuntimeType.FullName);
                return(BinderHelper.UnresolveableResult(restrictions, errorSuggestion));
            }

            var result = VelocityExpressions.BoxIfNeeded(
                Expression.Assign(
                    memberAccess,
                    VelocityExpressions.ConvertIfNeeded(value, memberAccess.Type)
                    )
                );

            return(new DynamicMetaObject(result, restrictions));
        }
コード例 #2
0
        public override DynamicMetaObject FallbackSetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (indexes == null)
            {
                throw new ArgumentNullException(nameof(indexes));
            }
            if (indexes == null)
            {
                throw new ArgumentNullException(nameof(indexes));
            }

            if (!target.HasValue || !value.HasValue || indexes.Any(x => !x.HasValue))
            {
                return(Defer(target, indexes.Concat(new[] { value }).ToArray()));
            }

            if (target.Value == null)
            {
                return(BinderHelper.NullTargetResult(target, errorSuggestion));
            }
            if (value.Value == null)
            {
                return(BinderHelper.SetNullValue(this, value));
            }

            var restrictions = BinderHelper.CreateCommonRestrictions(target, indexes)
                               .Merge(BinderHelper.CreateCommonRestrictions(value));

            var index = _indexerResolver.WriteableIndexer(target, indexes);

            if (index == null || !index.Type.IsAssignableFrom(value.RuntimeType))
            {
                return(BinderHelper.UnresolveableResult(restrictions, errorSuggestion));
            }

            var assignment = Expression.Assign(index, VelocityExpressions.ConvertIfNeeded(value.Expression, index.Type));

            return(new DynamicMetaObject(VelocityExpressions.ConvertIfNeeded(assignment, ReturnType), restrictions));
        }