Updated to r54115
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / MethodCallExpression.cs
index 4f8972a492cdd1945ab562c1c9c1f2f925a1a917..66b3bb001dc6832cb4be99567ab372f5ed4a1774 100644 (file)
@@ -2,11 +2,11 @@
  *
  * Copyright (c) Microsoft Corporation. 
  *
- * This source code is subject to terms and conditions of the Microsoft Public License. A 
+ * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  * copy of the license can be found in the License.html file at the root of this distribution. If 
- * you cannot locate the  Microsoft Public License, please send an email to 
+ * you cannot locate the  Apache License, Version 2.0, please send an email to 
  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
- * by the terms of the Microsoft Public License.
+ * by the terms of the Apache License, Version 2.0.
  *
  * You must not remove this notice, or any other, from this software.
  *
@@ -814,6 +814,9 @@ namespace System.Linq.Expressions {
 
         private static void ValidateStaticOrInstanceMethod(Expression instance, MethodInfo method) {
             if (method.IsStatic) {
+#if SILVERLIGHT
+                if (SilverlightQuirks) return;
+#endif
                 if (instance != null) throw new ArgumentException(Strings.OnlyStaticMethodsHaveNullInstance, "instance");
             } else {
                 if (instance == null) throw new ArgumentException(Strings.OnlyStaticMethodsHaveNullInstance, "method");
@@ -890,9 +893,7 @@ namespace System.Linq.Expressions {
             }
             TypeUtils.ValidateType(pType);
             if (!TypeUtils.AreReferenceAssignable(pType, arg.Type)) {
-                if (TypeUtils.IsSameOrSubclass(typeof(LambdaExpression), pType) && pType.IsAssignableFrom(arg.GetType())) {
-                    arg = Expression.Quote(arg);
-                } else {
+                if (!TryQuote(pType, ref arg)) {
                     // Throw the right error for the node we were given
                     switch (nodeKind) {
                         case ExpressionType.New:
@@ -910,6 +911,23 @@ namespace System.Linq.Expressions {
             return arg;
         }
 
+        // Attempts to auto-quote the expression tree. Returns true if it succeeded, false otherwise.
+        private static bool TryQuote(Type parameterType, ref Expression argument) {
+            // We used to allow quoting of any expression, but the behavior of
+            // quote (produce a new tree closed over parameter values), only
+            // works consistently for lambdas
+            Type quoteable = typeof(LambdaExpression);
+#if SILVERLIGHT
+            if (SilverlightQuirks) quoteable = typeof(Expression);
+#endif
+            if (TypeUtils.IsSameOrSubclass(quoteable, parameterType) &&
+                parameterType.IsAssignableFrom(argument.GetType())) {
+                argument = Expression.Quote(argument);
+                return true;
+            }
+            return false;
+        }
+
         private static MethodInfo FindMethod(Type type, string methodName, Type[] typeArgs, Expression[] args, BindingFlags flags) {
             MemberInfo[] members = type.FindMembers(MemberTypes.Method, flags, Type.FilterNameIgnoreCase, methodName);
             if (members == null || members.Length == 0)