-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathNullabilitySamples.cs
84 lines (67 loc) · 2.34 KB
/
NullabilitySamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma warning disable CS0649
#pragma warning disable CS8618
public class NullabilitySamples
{
#region NullabilityUsage
[Test]
public void Test()
{
var type = typeof(NullabilityTarget);
var arrayField = type.GetField("ArrayField")!;
var genericField = type.GetField("GenericField")!;
var context = new NullabilityInfoContext();
var arrayInfo = context.Create(arrayField);
Assert.AreEqual(NullabilityState.NotNull, arrayInfo.ReadState);
Assert.AreEqual(NullabilityState.Nullable, arrayInfo.ElementType!.ReadState);
var genericInfo = context.Create(genericField);
Assert.AreEqual(NullabilityState.NotNull, genericInfo.ReadState);
Assert.AreEqual(NullabilityState.NotNull, genericInfo.GenericTypeArguments[0].ReadState);
Assert.AreEqual(NullabilityState.Nullable, genericInfo.GenericTypeArguments[1].ReadState);
}
#endregion
#region NullabilityExtension
[Test]
public void ExtensionTests()
{
var type = typeof(NullabilityTarget);
var field = type.GetField("StringField")!;
Assert.True(field.IsNullable());
Assert.AreEqual(NullabilityState.Nullable, field.GetNullability());
Assert.AreEqual(NullabilityState.Nullable, field.GetNullabilityInfo().ReadState);
}
#endregion
// ReSharper disable UnusedMember.Local
// ReSharper disable NotAccessedField.Local
class PropertyTarget
{
string? write;
public string? ReadWrite { get; set; }
public string? Read { get; }
public string? Write
{
set => write = value;
}
}
// ReSharper restore NotAccessedField.Local
// ReSharper restore UnusedMember.Local
[Test]
public void Property()
{
var type = typeof(PropertyTarget);
var readWrite = type.GetProperty("ReadWrite")!;
var write = type.GetProperty("Write")!;
var read = type.GetProperty("Read")!;
Assert.True(readWrite.IsNullable());
Assert.True(write.IsNullable());
Assert.True(read.IsNullable());
}
#pragma warning disable CS0649, CS8618
#region NullabilityTarget
class NullabilityTarget
{
public string? StringField;
public string?[] ArrayField;
public Dictionary<string, object?> GenericField;
}
#endregion
}