NullableAttributes.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  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. #if INTERNAL_NULLABLE_ATTRIBUTES
  9. internal
  10. #else
  11. public
  12. #endif
  13. sealed class AllowNullAttribute : Attribute { }
  14. /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
  15. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
  16. #if INTERNAL_NULLABLE_ATTRIBUTES
  17. internal
  18. #else
  19. public
  20. #endif
  21. sealed class DisallowNullAttribute : Attribute { }
  22. /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
  23. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
  24. #if INTERNAL_NULLABLE_ATTRIBUTES
  25. internal
  26. #else
  27. public
  28. #endif
  29. sealed class MaybeNullAttribute : Attribute { }
  30. /// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
  31. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
  32. #if INTERNAL_NULLABLE_ATTRIBUTES
  33. internal
  34. #else
  35. public
  36. #endif
  37. sealed class NotNullAttribute : Attribute { }
  38. /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
  39. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  40. #if INTERNAL_NULLABLE_ATTRIBUTES
  41. internal
  42. #else
  43. public
  44. #endif
  45. sealed class MaybeNullWhenAttribute : Attribute
  46. {
  47. /// <summary>Initializes the attribute with the specified return value condition.</summary>
  48. /// <param name="returnValue">
  49. /// The return value condition. If the method returns this value, the associated parameter may be null.
  50. /// </param>
  51. public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
  52. /// <summary>Gets the return value condition.</summary>
  53. public bool ReturnValue { get; }
  54. }
  55. /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
  56. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  57. #if INTERNAL_NULLABLE_ATTRIBUTES
  58. internal
  59. #else
  60. public
  61. #endif
  62. sealed class NotNullWhenAttribute : Attribute
  63. {
  64. /// <summary>Initializes the attribute with the specified return value condition.</summary>
  65. /// <param name="returnValue">
  66. /// The return value condition. If the method returns this value, the associated parameter will not be null.
  67. /// </param>
  68. public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
  69. /// <summary>Gets the return value condition.</summary>
  70. public bool ReturnValue { get; }
  71. }
  72. /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
  73. [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
  74. #if INTERNAL_NULLABLE_ATTRIBUTES
  75. internal
  76. #else
  77. public
  78. #endif
  79. sealed class NotNullIfNotNullAttribute : Attribute
  80. {
  81. /// <summary>Initializes the attribute with the associated parameter name.</summary>
  82. /// <param name="parameterName">
  83. /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
  84. /// </param>
  85. public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
  86. /// <summary>Gets the associated parameter name.</summary>
  87. public string ParameterName { get; }
  88. }
  89. /// <summary>Applied to a method that will never return under any circumstance.</summary>
  90. [AttributeUsage(AttributeTargets.Method, Inherited = false)]
  91. #if INTERNAL_NULLABLE_ATTRIBUTES
  92. internal
  93. #else
  94. public
  95. #endif
  96. sealed class DoesNotReturnAttribute : Attribute { }
  97. /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
  98. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  99. #if INTERNAL_NULLABLE_ATTRIBUTES
  100. internal
  101. #else
  102. public
  103. #endif
  104. sealed class DoesNotReturnIfAttribute : Attribute
  105. {
  106. /// <summary>Initializes the attribute with the specified parameter value.</summary>
  107. /// <param name="parameterValue">
  108. /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
  109. /// the associated parameter matches this value.
  110. /// </param>
  111. public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
  112. /// <summary>Gets the condition parameter value.</summary>
  113. public bool ParameterValue { get; }
  114. }
  115. }