diff --git a/src/System.Linq.Expressions/src/System/Dynamic/Utils/TypeExtensions.cs b/src/System.Linq.Expressions/src/System/Dynamic/Utils/TypeExtensions.cs index d2365611cfdc..6fd93dd1518a 100644 --- a/src/System.Linq.Expressions/src/System/Dynamic/Utils/TypeExtensions.cs +++ b/src/System.Linq.Expressions/src/System/Dynamic/Utils/TypeExtensions.cs @@ -17,9 +17,14 @@ public static MethodInfo GetAnyStaticMethodValidated( string name, Type[] types) { - var method = type.GetAnyStaticMethod(name); - - return method.MatchesArgumentTypes(types) ? method : null; + foreach (MethodInfo method in type.GetTypeInfo().DeclaredMethods) + { + if (method.IsStatic && method.Name == name && method.MatchesArgumentTypes(types)) + { + return method; + } + } + return null; } /// @@ -105,17 +110,5 @@ public static IEnumerable GetStaticMethods(this Type type) } } } - - public static MethodInfo GetAnyStaticMethod(this Type type, string name) - { - foreach (var method in type.GetRuntimeMethods()) - { - if (method.IsStatic && method.Name == name) - { - return method; - } - } - return null; - } } } diff --git a/src/System.Linq.Expressions/tests/BinaryOperators/Arithmetic/BinarySubtractTests.cs b/src/System.Linq.Expressions/tests/BinaryOperators/Arithmetic/BinarySubtractTests.cs index f80b5d50cd92..40f4215fb6d9 100644 --- a/src/System.Linq.Expressions/tests/BinaryOperators/Arithmetic/BinarySubtractTests.cs +++ b/src/System.Linq.Expressions/tests/BinaryOperators/Arithmetic/BinarySubtractTests.cs @@ -474,6 +474,43 @@ private static void VerifyCharSubtract(char a, char b) #endregion + [Theory] + [ClassData(typeof(CompilationTypes))] + public static void Subtract_MultipleOverloads_CorrectlyResolvesOperator1(bool useInterpreter) + { + BinaryExpression subtract = Expression.Subtract(Expression.Constant(new DateTime(100)), Expression.Constant(new DateTime(10))); + Func lambda = Expression.Lambda>(subtract).Compile(useInterpreter); + Assert.Equal(new TimeSpan(90), lambda()); + } + + [Theory] + [ClassData(typeof(CompilationTypes))] + public static void Subtract_MultipleOverloads_CorrectlyResolvesOperator2(bool useInterpreter) + { + BinaryExpression subtract = Expression.Subtract(Expression.Constant(new DateTime(100)), Expression.Constant(new TimeSpan(10))); + Func lambda = Expression.Lambda>(subtract).Compile(useInterpreter); + Assert.Equal(new DateTime(90), lambda()); + } + + [Fact] + public static void Subtract_NoSuchOperatorDeclaredOnType_ThrowsInvalidOperationException() + { + Assert.Throws(() => Expression.Add(Expression.Constant(new SubClass(0)), Expression.Constant(new SubClass(1)))); + } + + public class BaseClass + { + public BaseClass(int value) { Value = value; } + public int Value { get; } + + public static BaseClass operator -(BaseClass i1, BaseClass i2) => new BaseClass(i1.Value - i2.Value); + } + + public class SubClass : BaseClass + { + public SubClass(int value) : base(value) { } + } + [Fact] public static void CannotReduce() { diff --git a/src/System.Linq.Expressions/tests/New/TypeExtensions.cs b/src/System.Linq.Expressions/tests/New/TypeExtensions.cs index bffd047bb8f5..e773d8892ce8 100644 --- a/src/System.Linq.Expressions/tests/New/TypeExtensions.cs +++ b/src/System.Linq.Expressions/tests/New/TypeExtensions.cs @@ -67,9 +67,14 @@ internal static MethodInfo GetAnyStaticMethodValidated( string name, Type[] types) { - var method = type.GetAnyStaticMethod(name); - - return method.MatchesArgumentTypes(types) ? method : null; + foreach (MethodInfo method in type.GetTypeInfo().DeclaredMethods) + { + if (method.IsStatic && method.Name == name && method.MatchesArgumentTypes(types)) + { + return method; + } + } + return null; } /// @@ -124,17 +129,5 @@ internal static bool AreReferenceAssignable(Type dest, Type src) } return false; } - - internal static MethodInfo GetAnyStaticMethod(this Type type, string name) - { - foreach (var method in type.GetTypeInfo().DeclaredMethods) - { - if (method.IsStatic && method.Name == name) - { - return method; - } - } - return null; - } } }