|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.IO;
|
| 5 | +using System.Linq.Expressions; |
5 | 6 | using System.Reflection;
|
6 | 7 | using System.Runtime.CompilerServices;
|
7 | 8 | using System.Runtime.InteropServices;
|
@@ -435,6 +436,50 @@ private static void IntIntMethod(int expected, int actual)
|
435 | 436 | Assert.Equal(expected, actual);
|
436 | 437 | }
|
437 | 438 |
|
| 439 | + [Fact] |
| 440 | + public static void SameMethodObtainedViaDelegateAndReflectionAreSameForClass() |
| 441 | + { |
| 442 | + var m1 = ((MethodCallExpression)((Expression<Action>)(() => new Class().M())).Body).Method; |
| 443 | + var m2 = new Action(new Class().M).Method; |
| 444 | + Assert.True(m1.Equals(m2)); |
| 445 | + } |
| 446 | + |
| 447 | + [Fact] |
| 448 | + public static void SameMethodObtainedViaDelegateAndReflectionAreSameForStruct() |
| 449 | + { |
| 450 | + var m1 = ((MethodCallExpression)((Expression<Action>)(() => new Struct().M())).Body).Method; |
| 451 | + var m2 = new Action(new Struct().M).Method; |
| 452 | + Assert.True(m1.Equals(m2)); |
| 453 | + } |
| 454 | + |
| 455 | + [Fact] |
| 456 | + public static void SameGenericMethodObtainedViaDelegateAndReflectionAreSameForClass() |
| 457 | + { |
| 458 | + var m1 = ((MethodCallExpression)((Expression<Action>)(() => new ClassG().M<string, object>())).Body).Method; |
| 459 | + var m2 = new Action(new ClassG().M<string, object>).Method; |
| 460 | + Assert.True(m1.Equals(m2)); |
| 461 | + Assert.True(m1.GetHashCode().Equals(m2.GetHashCode())); |
| 462 | + Assert.Equal(m1.MethodHandle.Value, m2.MethodHandle.Value); |
| 463 | + } |
| 464 | + |
| 465 | + [Fact] |
| 466 | + public static void SameGenericMethodObtainedViaDelegateAndReflectionAreSameForStruct() |
| 467 | + { |
| 468 | + var m1 = ((MethodCallExpression)((Expression<Action>)(() => new StructG().M<string, object>())).Body).Method; |
| 469 | + var m2 = new Action(new StructG().M<string, object>).Method; |
| 470 | + Assert.True(m1.Equals(m2)); |
| 471 | + Assert.True(m1.GetHashCode().Equals(m2.GetHashCode())); |
| 472 | + Assert.Equal(m1.MethodHandle.Value, m2.MethodHandle.Value); |
| 473 | + } |
| 474 | + |
| 475 | + class Class { internal void M() { } } |
| 476 | + |
| 477 | + struct Struct { internal void M() { } } |
| 478 | + |
| 479 | + class ClassG { internal void M<Key, Value>() { } } |
| 480 | + |
| 481 | + struct StructG { internal void M<Key, Value>() { } } |
| 482 | + |
438 | 483 | private delegate void IntIntDelegate(int expected, int actual);
|
439 | 484 | private delegate void IntIntDelegateWithDefault(int expected, int actual = 7);
|
440 | 485 |
|
|
0 commit comments