示例#1
0
        public static Expression BuildConversionCall(this IExpressionGenerator gen, MethodInfo method, Type resultType, params Expression[] inputExpressions)
        {
            Contract.Requires(gen != null);
            Contract.Requires(method != null);
            Contract.Requires(inputExpressions != null);
            Contract.Requires(Contract.ForAll(inputExpressions, x => x != null));

            var methodParams = method.GetParameters();

            if (methodParams.Length != inputExpressions.Length)
            {
                throw new ArgumentException(String.Format("{0} input expressions are required.", methodParams.Length));
            }

            for (int i = 0; i < methodParams.Length; i++)
            {
                Contract.Assume(methodParams[i] != null);
                var paramType       = methodParams[i].ParameterType;
                var inputExpression = inputExpressions[i];
                if (inputExpression.Type != paramType)
                {
                    inputExpressions[i] = gen.GenerateConversionOrThrow(paramType, inputExpression);
                }
            }

            Expression result = method.BuildCallExpression(inputExpressions);

            if (resultType != null && result.Type != resultType)
            {
                result = gen.GenerateConversionOrThrow(resultType, result);
            }

            return(result);
        }
示例#2
0
        public static Expression BuildConversionCall(this IExpressionGenerator gen, MethodInfo method, Expression input, Type resultType)
        {
            Contract.Requires(gen != null);
            Contract.Requires(method != null);
            Contract.Requires(input != null);

            var methodParams = method.GetParameters();

            if (methodParams.Length > 0)
            {
                Contract.Assume(methodParams[0] != null);
                var paramType = methodParams[0].ParameterType;
                if (input.Type != paramType)
                {
                    input = gen.GenerateConversionOrThrow(paramType, input);
                }
            }

            Expression result = method.BuildCallExpression(input);

            if (resultType != null && result.Type != resultType)
            {
                result = gen.GenerateConversionOrThrow(resultType, result);
            }

            return(result);
        }
示例#3
0
        Action <object, object> CreateAddDelegate(object instance, object item, Type collectionType, Type itemType, Tuple <Type, Type> key, XamlInvokerOptions mode)
        {
            // this is separate as the anonymous types are instantiated at the beginning of the method
            Action <object, object> addDelegate;
            MethodInfo mi = LookupAddCollectionMethod(Type, collectionType, itemType);

            if (mi == null)
            {
                throw new InvalidOperationException($"The collection type '{collectionType}' does not have 'Add' method");
            }

            if (mode.HasFlag(XamlInvokerOptions.DeferCompile))
            {
                cache[key] = addDelegate = (i, v) => mi.Invoke(i, new object[] { v });
                Task.Factory.StartNew(() => cache[key] = addDelegate = mi.BuildCallExpression());
            }
            else
            {
                cache[key] = addDelegate = mi.BuildCallExpression();
            }
            return(addDelegate);
        }