Skip to content

Commit 8d1bcbd

Browse files
committed
Introduced polyfill for [MemberNotNullWhen] attribute
The `[MemberNotNullWhen]` attribute is introduced in .NET 5, and not available in .NET Standard 2.1, which OnTopic targets. We could multi-target .NET 5 to gain access to it. But as it's one of the few .NET 5 capabilities we have an immediate need for, and it can easily be introduced via a polyfill, we're doing that. This allows us to annotate e.g. properties as a means of validating that local members—including private fields—will not be null after the annotated member is called, assuming a given return value.
1 parent 01ad3c9 commit 8d1bcbd

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*==============================================================================================================================
2+
| Author Ignia, LLC
3+
| Client Ignia, LLC
4+
| Project Topics Library
5+
\=============================================================================================================================*/
6+
namespace System.Diagnostics.CodeAnalysis {
7+
8+
/*============================================================================================================================
9+
| CLASS: MEMBER NOT NULL (ATTRIBUTE)
10+
\---------------------------------------------------------------------------------------------------------------------------*/
11+
/// <summary>
12+
/// Specifies that the method or property will ensure that the listed field and property members have not-null values when
13+
/// returning with the specified return value condition.
14+
/// </summary>
15+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
16+
internal sealed class MemberNotNullWhenAttribute: Attribute {
17+
18+
/*==========================================================================================================================
19+
| CONSTRUCTOR
20+
\-------------------------------------------------------------------------------------------------------------------------*/
21+
/// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
22+
/// <param name="returnValue">
23+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
24+
/// </param>
25+
/// <param name="member">
26+
/// The field or property member that is promised to be not-null.
27+
/// </param>
28+
#pragma warning disable CA1019 // Define accessors for attribute arguments
29+
public MemberNotNullWhenAttribute(bool returnValue, string member) {
30+
ReturnValue = returnValue;
31+
Members = new[] { member };
32+
}
33+
#pragma warning restore CA1019 // Define accessors for attribute arguments
34+
35+
/// <summary>
36+
/// Initializes the attribute with the specified return value condition and list of field and property members.
37+
/// </summary>
38+
/// <param name="returnValue">
39+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
40+
/// </param>
41+
/// <param name="members">
42+
/// The list of field and property members that are promised to be not-null.
43+
/// </param>
44+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) {
45+
ReturnValue = returnValue;
46+
Members = members;
47+
}
48+
49+
/*==========================================================================================================================
50+
| PROPERTY: RETURN VALUE
51+
\-------------------------------------------------------------------------------------------------------------------------*/
52+
/// <summary>Gets the return value condition.</summary>
53+
public bool ReturnValue { get; }
54+
55+
/*==========================================================================================================================
56+
| PROPERTY: MEMBERS
57+
\-------------------------------------------------------------------------------------------------------------------------*/
58+
/// <summary>Gets field or property member names.</summary>
59+
public string[] Members { get; }
60+
61+
}
62+
}

0 commit comments

Comments
 (0)