예제 #1
0
        public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (!target.HasValue || args.Any(x => !x.HasValue))
            {
                return(Defer(target, args));
            }

            if (target == null)
            {
                return(BinderHelper.NullTargetResult(target, errorSuggestion));
            }


            // If an argument has a null value, use a null type so that the resolution algorithm can do implicit null conversions
            var argTypes = args.Select(x => x.Value == null ? null : x.RuntimeType).ToImmutableList();

            OverloadResolutionData <MethodInfo> method = _methodResolver.ResolveMethod(target.LimitType.GetTypeInfo(), Name, argTypes);

            var restrictions = BinderHelper.CreateCommonRestrictions(target, args);

            if (method == null)
            {
                var log = BindingEventSource.Log;
                if (log.IsEnabled())
                {
                    var argTypeString = string.Join(",", argTypes.Select(x => x.FullName).ToArray());
                    log.InvokeMemberResolutionFailure(Name, target.LimitType.FullName, argTypeString);
                }

                return(BinderHelper.UnresolveableResult(restrictions, errorSuggestion));
            }

            var methodExpression = _methodResolver.ConvertMethodParameters(method, target.Expression, args);

            return(new DynamicMetaObject(
                       VelocityExpressions.ConvertIfNeeded(methodExpression, ReturnType),
                       restrictions
                       ));
        }
        public override Expression Method(Expression target, string name, IImmutableList <Expression> args, SourceInfo sourceInfo)
        {
            if (IsConstantType(target) && args.All(IsConstantType))
            {
                var methodInfo = _methodResolver.ResolveMethod(target.Type.GetTypeInfo(), name, args.Select(x => x.Type).ToImmutableArray());
                //TODO: Include debug info
                if (methodInfo == null)
                {
                    return(Constants.NullExpression);
                }

                return(_methodResolver.ConvertMethodParameters(methodInfo, target, args.Select(x => new DynamicMetaObject(x, BindingRestrictions.Empty)).ToArray())
                       ?? Constants.NullExpression);
            }

            return(base.Method(target, name, args, sourceInfo));
        }