1+
2+ #if NETSTANDARD2_0 || NETFRAMEWORK
3+
4+ namespace System . Diagnostics . CodeAnalysis ;
5+
6+ /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
7+ [ AttributeUsage ( AttributeTargets . Field | AttributeTargets . Parameter | AttributeTargets . Property , Inherited = false ) ]
8+ internal sealed class AllowNullAttribute : Attribute { }
9+
10+ /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
11+ [ AttributeUsage ( AttributeTargets . Field | AttributeTargets . Parameter | AttributeTargets . Property , Inherited = false ) ]
12+ internal sealed class DisallowNullAttribute : Attribute { }
13+
14+ /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
15+ [ AttributeUsage ( AttributeTargets . Field | AttributeTargets . Parameter | AttributeTargets . Property | AttributeTargets . ReturnValue , Inherited = false ) ]
16+ internal sealed class MaybeNullAttribute : Attribute { }
17+
18+ /// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
19+ [ AttributeUsage ( AttributeTargets . Field | AttributeTargets . Parameter | AttributeTargets . Property | AttributeTargets . ReturnValue , Inherited = false ) ]
20+ internal sealed class NotNullAttribute : Attribute { }
21+
22+ /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
23+ [ AttributeUsage ( AttributeTargets . Parameter , Inherited = false ) ]
24+ internal sealed class MaybeNullWhenAttribute : Attribute
25+ {
26+ /// <summary>Initializes the attribute with the specified return value condition.</summary>
27+ /// <param name="returnValue">
28+ /// The return value condition. If the method returns this value, the associated parameter may be null.
29+ /// </param>
30+ public MaybeNullWhenAttribute ( bool returnValue ) => ReturnValue = returnValue ;
31+
32+ /// <summary>Gets the return value condition.</summary>
33+ public bool ReturnValue { get ; }
34+ }
35+
36+ /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
37+ [ AttributeUsage ( AttributeTargets . Parameter , Inherited = false ) ]
38+ internal sealed class NotNullWhenAttribute : Attribute
39+ {
40+ /// <summary>Initializes the attribute with the specified return value condition.</summary>
41+ /// <param name="returnValue">
42+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
43+ /// </param>
44+ public NotNullWhenAttribute ( bool returnValue ) => ReturnValue = returnValue ;
45+
46+ /// <summary>Gets the return value condition.</summary>
47+ public bool ReturnValue { get ; }
48+ }
49+
50+ /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
51+ [ AttributeUsage ( AttributeTargets . Parameter | AttributeTargets . Property | AttributeTargets . ReturnValue , AllowMultiple = true , Inherited = false ) ]
52+ internal sealed class NotNullIfNotNullAttribute : Attribute
53+ {
54+ /// <summary>Initializes the attribute with the associated parameter name.</summary>
55+ /// <param name="parameterName">
56+ /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
57+ /// </param>
58+ public NotNullIfNotNullAttribute ( string parameterName ) => ParameterName = parameterName ;
59+
60+ /// <summary>Gets the associated parameter name.</summary>
61+ public string ParameterName { get ; }
62+ }
63+
64+ /// <summary>Applied to a method that will never return under any circumstance.</summary>
65+ [ AttributeUsage ( AttributeTargets . Method , Inherited = false ) ]
66+ internal sealed class DoesNotReturnAttribute : Attribute { }
67+
68+ /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
69+ [ AttributeUsage ( AttributeTargets . Parameter , Inherited = false ) ]
70+ internal sealed class DoesNotReturnIfAttribute : Attribute
71+ {
72+ /// <summary>Initializes the attribute with the specified parameter value.</summary>
73+ /// <param name="parameterValue">
74+ /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
75+ /// the associated parameter matches this value.
76+ /// </param>
77+ public DoesNotReturnIfAttribute ( bool parameterValue ) => ParameterValue = parameterValue ;
78+
79+ /// <summary>Gets the condition parameter value.</summary>
80+ public bool ParameterValue { get ; }
81+ }
82+
83+
84+ /// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
85+ [ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Property , Inherited = false , AllowMultiple = true ) ]
86+ internal sealed class MemberNotNullAttribute : Attribute
87+ {
88+ /// <summary>Initializes the attribute with a field or property member.</summary>
89+ /// <param name="member">
90+ /// The field or property member that is promised to be not-null.
91+ /// </param>
92+ public MemberNotNullAttribute ( string member ) => Members = new [ ] { member } ;
93+
94+ /// <summary>Initializes the attribute with the list of field and property members.</summary>
95+ /// <param name="members">
96+ /// The list of field and property members that are promised to be not-null.
97+ /// </param>
98+ public MemberNotNullAttribute ( params string [ ] members ) => Members = members ;
99+
100+ /// <summary>Gets field or property member names.</summary>
101+ public string [ ] Members { get ; }
102+ }
103+
104+ /// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
105+ [ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Property , Inherited = false , AllowMultiple = true ) ]
106+ internal sealed class MemberNotNullWhenAttribute : Attribute
107+ {
108+ /// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
109+ /// <param name="returnValue">
110+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
111+ /// </param>
112+ /// <param name="member">
113+ /// The field or property member that is promised to be not-null.
114+ /// </param>
115+ public MemberNotNullWhenAttribute ( bool returnValue , string member )
116+ {
117+ ReturnValue = returnValue ;
118+ Members = new [ ] { member } ;
119+ }
120+
121+ /// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
122+ /// <param name="returnValue">
123+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
124+ /// </param>
125+ /// <param name="members">
126+ /// The list of field and property members that are promised to be not-null.
127+ /// </param>
128+ public MemberNotNullWhenAttribute ( bool returnValue , params string [ ] members )
129+ {
130+ ReturnValue = returnValue ;
131+ Members = members ;
132+ }
133+
134+ /// <summary>Gets the return value condition.</summary>
135+ public bool ReturnValue { get ; }
136+
137+ /// <summary>Gets field or property member names.</summary>
138+ public string [ ] Members { get ; }
139+ }
140+ #endif
141+
142+ #if NETSTANDARD2_1
143+
144+ namespace System . Diagnostics . CodeAnalysis ;
145+
146+ /// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>
147+ [ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Property , Inherited = false , AllowMultiple = true ) ]
148+ internal sealed class MemberNotNullAttribute : Attribute
149+ {
150+ /// <summary>Initializes the attribute with a field or property member.</summary>
151+ /// <param name="member">
152+ /// The field or property member that is promised to be not-null.
153+ /// </param>
154+ public MemberNotNullAttribute ( string member ) => Members = new [ ] { member } ;
155+
156+ /// <summary>Initializes the attribute with the list of field and property members.</summary>
157+ /// <param name="members">
158+ /// The list of field and property members that are promised to be not-null.
159+ /// </param>
160+ public MemberNotNullAttribute ( params string [ ] members ) => Members = members ;
161+
162+ /// <summary>Gets field or property member names.</summary>
163+ public string [ ] Members { get ; }
164+ }
165+
166+ /// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.</summary>
167+ [ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Property , Inherited = false , AllowMultiple = true ) ]
168+ internal sealed class MemberNotNullWhenAttribute : Attribute
169+ {
170+ /// <summary>Initializes the attribute with the specified return value condition and a field or property member.</summary>
171+ /// <param name="returnValue">
172+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
173+ /// </param>
174+ /// <param name="member">
175+ /// The field or property member that is promised to be not-null.
176+ /// </param>
177+ public MemberNotNullWhenAttribute ( bool returnValue , string member )
178+ {
179+ ReturnValue = returnValue ;
180+ Members = new [ ] { member } ;
181+ }
182+
183+ /// <summary>Initializes the attribute with the specified return value condition and list of field and property members.</summary>
184+ /// <param name="returnValue">
185+ /// The return value condition. If the method returns this value, the associated parameter will not be null.
186+ /// </param>
187+ /// <param name="members">
188+ /// The list of field and property members that are promised to be not-null.
189+ /// </param>
190+ public MemberNotNullWhenAttribute ( bool returnValue , params string [ ] members )
191+ {
192+ ReturnValue = returnValue ;
193+ Members = members ;
194+ }
195+
196+ /// <summary>Gets the return value condition.</summary>
197+ public bool ReturnValue { get ; }
198+
199+ /// <summary>Gets field or property member names.</summary>
200+ public string [ ] Members { get ; }
201+ }
202+
203+ #endif
0 commit comments