Skip to content

Commit 9db30e4

Browse files
committed
Annotate valid code
1 parent 0867ef0 commit 9db30e4

14 files changed

Lines changed: 94 additions & 94 deletions

ValidCode/Accessors.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ReSharper disable All
1+
// ReSharper disable All
22
namespace ValidCode
33
{
44
using System;
@@ -17,16 +17,16 @@ public void Valid()
1717
var instance = new Accessors { Bar = 1 };
1818
#pragma warning disable REFL014
1919
Assert.NotNull(typeof(Accessors).GetMethod("get_Bar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
20-
Assert.AreEqual(1, typeof(Accessors).GetMethod("get_Bar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Invoke(instance, null));
20+
Assert.AreEqual(1, typeof(Accessors).GetMethod("get_Bar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)?.Invoke(instance, null));
2121

2222
Assert.NotNull(typeof(Accessors).GetMethod("set_Bar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
23-
Assert.Null(typeof(Accessors).GetMethod("set_Bar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Invoke(instance, new object[] { 1 }));
23+
Assert.Null(typeof(Accessors).GetMethod("set_Bar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)?.Invoke(instance, new object[] { 1 }));
2424

2525
Assert.NotNull(typeof(Accessors).GetMethod("add_Baz", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
26-
Assert.Null(typeof(Accessors).GetMethod("add_Baz", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Invoke(instance, new object[] { new EventHandler((_, __) => { }) }));
26+
Assert.Null(typeof(Accessors).GetMethod("add_Baz", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)?.Invoke(instance, new object[] { new EventHandler((_, __) => { }) }));
2727

2828
Assert.NotNull(typeof(Accessors).GetMethod("remove_Baz", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
29-
Assert.Null(typeof(Accessors).GetMethod("remove_Baz", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Invoke(instance, new object[] { new EventHandler((_, __) => { }) }));
29+
Assert.Null(typeof(Accessors).GetMethod("remove_Baz", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)?.Invoke(instance, new object[] { new EventHandler((_, __) => { }) }));
3030
#pragma warning restore REFL014
3131
}
3232
}

ValidCode/ActivatorCreateInstance.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ public class ActivatorCreateInstance
1010
public void Valid()
1111
{
1212
Assert.NotNull(Activator.CreateInstance<ImplicitDefaultConstructor>());
13-
Assert.NotNull((ImplicitDefaultConstructor)Activator.CreateInstance(typeof(ImplicitDefaultConstructor)));
14-
Assert.NotNull((ImplicitDefaultConstructor)Activator.CreateInstance(typeof(ImplicitDefaultConstructor), true));
15-
Assert.NotNull((ImplicitDefaultConstructor)Activator.CreateInstance(typeof(ImplicitDefaultConstructor), false));
13+
Assert.NotNull((ImplicitDefaultConstructor?)Activator.CreateInstance(typeof(ImplicitDefaultConstructor)));
14+
Assert.NotNull((ImplicitDefaultConstructor?)Activator.CreateInstance(typeof(ImplicitDefaultConstructor), true));
15+
Assert.NotNull((ImplicitDefaultConstructor?)Activator.CreateInstance(typeof(ImplicitDefaultConstructor), false));
1616

1717
Assert.NotNull(Activator.CreateInstance<ExplicitDefaultConstructor>());
18-
Assert.NotNull((ExplicitDefaultConstructor)Activator.CreateInstance(typeof(ExplicitDefaultConstructor)));
19-
Assert.NotNull((ExplicitDefaultConstructor)Activator.CreateInstance(typeof(ExplicitDefaultConstructor), true));
20-
Assert.NotNull((ExplicitDefaultConstructor)Activator.CreateInstance(typeof(ExplicitDefaultConstructor), false));
18+
Assert.NotNull((ExplicitDefaultConstructor?)Activator.CreateInstance(typeof(ExplicitDefaultConstructor)));
19+
Assert.NotNull((ExplicitDefaultConstructor?)Activator.CreateInstance(typeof(ExplicitDefaultConstructor), true));
20+
Assert.NotNull((ExplicitDefaultConstructor?)Activator.CreateInstance(typeof(ExplicitDefaultConstructor), false));
2121

22-
Assert.NotNull((PrivateDefaultConstructor)Activator.CreateInstance(typeof(PrivateDefaultConstructor), true));
22+
Assert.NotNull((PrivateDefaultConstructor?)Activator.CreateInstance(typeof(PrivateDefaultConstructor), true));
2323

24-
Assert.NotNull((SingleDoubleParameter)Activator.CreateInstance(typeof(SingleDoubleParameter), 1));
25-
Assert.NotNull((SingleDoubleParameter)Activator.CreateInstance(typeof(SingleDoubleParameter), 1.2));
26-
Assert.NotNull((SingleDoubleParameter)Activator.CreateInstance(typeof(SingleDoubleParameter), new object[] { 1 }));
27-
Assert.NotNull((SingleDoubleParameter)Activator.CreateInstance(typeof(SingleDoubleParameter), new object[] { 1.2 }));
24+
Assert.NotNull((SingleDoubleParameter?)Activator.CreateInstance(typeof(SingleDoubleParameter), 1));
25+
Assert.NotNull((SingleDoubleParameter?)Activator.CreateInstance(typeof(SingleDoubleParameter), 1.2));
26+
Assert.NotNull((SingleDoubleParameter?)Activator.CreateInstance(typeof(SingleDoubleParameter), new object[] { 1 }));
27+
Assert.NotNull((SingleDoubleParameter?)Activator.CreateInstance(typeof(SingleDoubleParameter), new object[] { 1.2 }));
2828
}
2929

3030
public T Create<T>() => Activator.CreateInstance<T>();
3131

32-
public static object Foo<T>(object _) => Activator.CreateInstance(typeof(T), "foo");
32+
public static object? Foo<T>(object _) => Activator.CreateInstance(typeof(T), "foo");
3333

34-
public static object Foo<T>() => Activator.CreateInstance(typeof(T));
34+
public static object? Foo<T>() => Activator.CreateInstance(typeof(T));
3535

3636
public class ImplicitDefaultConstructor
3737
{

ValidCode/AssemblyLoad.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public class AssemblyLoad
77
{
88
public AssemblyLoad()
99
{
10-
_ = (string)Assembly.Load("SomeAssembly").GetType("SomeType")?.GetMethod("SomeMethod").Invoke(null, new object[] { 1 })!;
11-
_ = (string?)Assembly.Load("SomeAssembly").GetType("SomeType")?.GetMethod("SomeMethod").Invoke(null, new object[] { 1 });
12-
_ = (string)Assembly.Load("SomeAssembly").GetType("SomeType", throwOnError: true).GetMethod("SomeMethod").Invoke(null, new object[] { 1 });
13-
_ = (string)Assembly.Load("SomeAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e000\"").GetType("SomeType", throwOnError: true).GetMethod("SomeMethod").Invoke(null, new object[] { 1 });
10+
_ = (string)Assembly.Load("SomeAssembly").GetType("SomeType")?.GetMethod("SomeMethod")?.Invoke(null, new object[] { 1 })!;
11+
_ = (string?)Assembly.Load("SomeAssembly").GetType("SomeType")?.GetMethod("SomeMethod")?.Invoke(null, new object[] { 1 });
12+
_ = (string?)Assembly.Load("SomeAssembly").GetType("SomeType", throwOnError: true)?.GetMethod("SomeMethod")?.Invoke(null, new object[] { 1 });
13+
_ = (string?)Assembly.Load("SomeAssembly, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e000\"").GetType("SomeType", throwOnError: true)?.GetMethod("SomeMethod")?.Invoke(null, new object[] { 1 });
1414
}
1515
}
1616
}

ValidCode/ConstructorInfoInvoke.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ReSharper disable All
1+
// ReSharper disable All
22
namespace ValidCode
33
{
44
using System;
@@ -11,13 +11,13 @@ public class ConstructorInfoInvoke
1111
[Test]
1212
public void Valid()
1313
{
14-
Assert.NotNull(typeof(ConstructorInfoInvoke).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null).Invoke(null));
15-
Assert.NotNull(typeof(ConstructorInfoInvoke).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int) }, null).Invoke(new object[] { 1 }));
14+
Assert.NotNull(typeof(ConstructorInfoInvoke).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null)?.Invoke(null));
15+
Assert.NotNull(typeof(ConstructorInfoInvoke).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int) }, null)?.Invoke(new object[] { 1 }));
1616

1717
var type = typeof(ConstructorInfoInvoke);
1818
var instance = FormatterServices.GetUninitializedObject(type);
19-
Assert.Null(type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null).Invoke(instance, null));
20-
Assert.Null(type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int) }, null).Invoke(instance, new object[] { 1 }));
19+
Assert.Null(type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null)?.Invoke(instance, null));
20+
Assert.Null(type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(int) }, null)?.Invoke(instance, new object[] { 1 }));
2121
}
2222

2323
public ConstructorInfoInvoke()

ValidCode/DelegateCreateDelegate/CustomDelegate.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ReSharper disable All
1+
// ReSharper disable All
22
namespace ValidCode.DelegateCreateDelegate
33
{
44
using System;
@@ -17,42 +17,42 @@ public void Valid()
1717
{
1818
Assert.AreEqual(3, ((StringInt)Delegate.CreateDelegate(
1919
typeof(StringInt),
20-
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)))
20+
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!))
2121
.Invoke("abc"));
2222

23-
Assert.AreEqual(3, ((StringInt)Delegate.CreateDelegate(
23+
Assert.AreEqual(3, ((StringInt?)Delegate.CreateDelegate(
2424
typeof(StringInt),
25-
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null),
25+
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!,
2626
throwOnBindFailure: true))
27-
.Invoke("abc"));
27+
?.Invoke("abc"));
2828

2929
Assert.AreEqual(3, ((EmptyInt)Delegate.CreateDelegate(
3030
typeof(EmptyInt),
3131
"abc",
32-
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)))
32+
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!))
3333
.Invoke());
3434

35-
Assert.AreEqual(3, ((EmptyInt)Delegate.CreateDelegate(
35+
Assert.AreEqual(3, ((EmptyInt?)Delegate.CreateDelegate(
3636
typeof(EmptyInt),
3737
"abc",
38-
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null),
38+
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!,
3939
throwOnBindFailure: true))
40-
.Invoke());
40+
?.Invoke());
4141

4242
((Void)Delegate.CreateDelegate(
4343
typeof(Void),
44-
typeof(C).GetMethod(nameof(C.Void), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null)))
44+
typeof(C).GetMethod(nameof(C.Void), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null)!))
4545
.Invoke();
4646

4747
((IntVoid)Delegate.CreateDelegate(
4848
typeof(IntVoid),
49-
typeof(C).GetMethod(nameof(C.IntVoid), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(int) }, null)))
49+
typeof(C).GetMethod(nameof(C.IntVoid), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(int) }, null)!))
5050
.Invoke(1);
5151

5252
((Void)Delegate.CreateDelegate(
5353
typeof(Void),
5454
"abc",
55-
typeof(C).GetMethod(nameof(C.StringVoid), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)))
55+
typeof(C).GetMethod(nameof(C.StringVoid), BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!))
5656
.Invoke();
5757
}
5858

ValidCode/DelegateCreateDelegate/Instance.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ReSharper disable All
1+
// ReSharper disable All
22
namespace ValidCode.DelegateCreateDelegate
33
{
44
using System;
@@ -12,49 +12,49 @@ public void Valid()
1212
{
1313
Assert.AreEqual(3, ((Func<C, string, int>)Delegate.CreateDelegate(
1414
typeof(Func<C, string, int>),
15-
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)))
15+
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!))
1616
.Invoke(new C(), "abc"));
1717

1818
Assert.AreEqual(3, ((Func<string, int>)Delegate.CreateDelegate(
1919
typeof(Func<string, int>),
2020
new C(),
21-
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)))
21+
typeof(C).GetMethod(nameof(C.StringInt), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!))
2222
.Invoke("abc"));
2323

2424
((Action<C, int>)Delegate.CreateDelegate(
2525
typeof(Action<C, int>),
26-
typeof(C).GetMethod(nameof(C.IntVoid), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(int) }, null)))
26+
typeof(C).GetMethod(nameof(C.IntVoid), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(int) }, null)!))
2727
.Invoke(new C(), 1);
2828

2929
((Action)Delegate.CreateDelegate(
3030
typeof(Action),
3131
new C(),
32-
typeof(C).GetMethod(nameof(C.Void), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null)))
32+
typeof(C).GetMethod(nameof(C.Void), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null)!))
3333
.Invoke();
3434

3535
((Action<int>)Delegate.CreateDelegate(
3636
typeof(Action<int>),
3737
new C(),
38-
typeof(C).GetMethod(nameof(C.IntVoid), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(int) }, null)))
38+
typeof(C).GetMethod(nameof(C.IntVoid), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(int) }, null)!))
3939
.Invoke(1);
4040

4141
((Action<string>)Delegate.CreateDelegate(
4242
typeof(Action<string>),
4343
new C(),
44-
typeof(C).GetMethod(nameof(C.StringVoid), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)))
44+
typeof(C).GetMethod(nameof(C.StringVoid), BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, new[] { typeof(string) }, null)!))
4545
.Invoke("abc");
4646

4747
Assert.AreEqual(1, ((Func<C, int>)Delegate.CreateDelegate(
4848
typeof(Func<C, int>),
4949
typeof(C).GetProperty(
5050
nameof(C.Value),
51-
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetMethod)).Invoke(new C()));
51+
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)!.GetMethod!)).Invoke(new C()));
5252

5353
Assert.AreEqual(1, ((Func<C, int>)Delegate.CreateDelegate(
5454
typeof(Func<C, int>),
5555
typeof(C).GetProperty(
5656
nameof(C.Value),
57-
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetGetMethod())).Invoke(new C()));
57+
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly!)!.GetGetMethod()!)).Invoke(new C()));
5858
}
5959

6060
private class C

0 commit comments

Comments
 (0)