Skip to content

Commit 01ad3c9

Browse files
committed
Introduced polyfill for [MemberNotNull] attribute
The `[MemberNotNull]` 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.
1 parent c15bf2e commit 01ad3c9

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.
13+
/// </summary>
14+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
15+
internal sealed class MemberNotNullAttribute : Attribute {
16+
17+
/*==========================================================================================================================
18+
| CONSTRUCTOR
19+
\-------------------------------------------------------------------------------------------------------------------------*/
20+
/// <summary>Initializes the attribute with a field or property member.</summary>
21+
/// <param name="member">
22+
/// The field or property member that is promised to be not-null.
23+
/// </param>
24+
#pragma warning disable CA1019 // Define accessors for attribute arguments
25+
public MemberNotNullAttribute(string member) {
26+
Members = new[] { member };
27+
}
28+
#pragma warning restore CA1019 // Define accessors for attribute arguments
29+
30+
/// <summary>Initializes the attribute with the list of field and property members.</summary>
31+
/// <param name="members">
32+
/// The list of field and property members that are promised to be not-null.
33+
/// </param>
34+
public MemberNotNullAttribute(params string[] members) {
35+
Members = members;
36+
}
37+
38+
/*==========================================================================================================================
39+
| PROPERTY: MEMBERS
40+
\-------------------------------------------------------------------------------------------------------------------------*/
41+
/// <summary>Gets field or property member names.</summary>
42+
public string[] Members { get; }
43+
44+
}
45+
}

0 commit comments

Comments
 (0)