@@ -11,33 +11,69 @@ public static class MethodInfoInvoke
1111 private static readonly CastReturnValueFix Fix = new ( ) ;
1212 private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic . Create ( Descriptors . REFL001CastReturnValue ) ;
1313
14- [ Test ]
15- public static void AssigningLocal ( )
14+ [ TestCase ( "typeof(C).GetMethod(nameof(M)).Invoke(null, null)" ) ]
15+ [ TestCase ( "typeof(C).GetMethod(nameof(M))!.Invoke(null, null)" ) ]
16+ [ TestCase ( "typeof(C).GetMethod(nameof(M))?.Invoke(null, null)" ) ]
17+ [ TestCase ( "typeof(C).GetMethod(nameof(M))?.Invoke(null, null) ?? throw new Exception()" ) ]
18+ public static void AssigningLocal ( string expression )
1619 {
1720 var before = @"
21+ #pragma warning disable CS8019, CS8602, CS8605
1822namespace N
1923{
24+ using System;
25+
2026 public class C
2127 {
2228 public C()
2329 {
24- var value = ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
30+ var x = ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
2531 }
2632
2733 public static int M() => 0;
2834 }
29- }" ;
35+ }" . AssertReplace ( "typeof(C).GetMethod(nameof(M)).Invoke(null, null)" , expression ) ;
3036
3137 var after = @"
38+ #pragma warning disable CS8019, CS8602, CS8605
3239namespace N
3340{
41+ using System;
42+
3443 public class C
3544 {
3645 public C()
3746 {
38- var value = (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
47+ var x = (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
3948 }
4049
50+ public static int M() => 0;
51+ }
52+ }" . AssertReplace ( "typeof(C).GetMethod(nameof(M)).Invoke(null, null)" , expression ) ;
53+ RoslynAssert . CodeFix ( Analyzer , Fix , ExpectedDiagnostic , before , after ) ;
54+ }
55+
56+ [ Test ]
57+ public static void Returning ( )
58+ {
59+ var before = @"
60+ namespace N
61+ {
62+ public class C
63+ {
64+ public object? Get() => ↓typeof(C).GetMethod(nameof(M)).Invoke(null, null);
65+
66+ public static int M() => 0;
67+ }
68+ }" ;
69+
70+ var after = @"
71+ namespace N
72+ {
73+ public class C
74+ {
75+ public object? Get() => (int)typeof(C).GetMethod(nameof(M)).Invoke(null, null);
76+
4177 public static int M() => 0;
4278 }
4379}" ;
0 commit comments