Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand Down Expand Up @@ -105,17 +110,5 @@ public static IEnumerable<MethodInfo> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimeSpan> lambda = Expression.Lambda<Func<TimeSpan>>(subtract).Compile(useInterpreter);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add the second subtract expression which subtract a TimeSpan from DateTime to show that there are 2 "op_Subtract" methods and both work properly.

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<DateTime> lambda = Expression.Lambda<Func<DateTime>>(subtract).Compile(useInterpreter);
Assert.Equal(new DateTime(90), lambda());
}

[Fact]
public static void Subtract_NoSuchOperatorDeclaredOnType_ThrowsInvalidOperationException()
{
Assert.Throws<InvalidOperationException>(() => 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()
{
Expand Down
23 changes: 8 additions & 15 deletions src/System.Linq.Expressions/tests/New/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand Down Expand Up @@ -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;
}
}
}