You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
3.4 KiB

  1. #if !NETSTANDARD2_1
  2. namespace System.Diagnostics.CodeAnalysis
  3. {
  4. // Effectively the Microsoft implementation for when it doesn't exist for my convenience
  5. /// <summary>
  6. /// Specifies that when a method returns <see cref="ReturnValue"/>,
  7. /// the parameter may be <see langword="null"/> even if the corresponding type disallows it.
  8. /// </summary>
  9. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  10. internal sealed class MaybeNullWhenAttribute : Attribute
  11. {
  12. /// <summary>
  13. /// Initializes the attribute with the specified return value condition.
  14. /// </summary>
  15. /// <param name="returnValue">The return value condition. If the method returns this
  16. /// value, the associated parameter may be null.</param>
  17. public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
  18. /// <summary>
  19. /// Gets the return value condition.
  20. /// </summary>
  21. /// <value>The return value condition. If the method returns this value, the
  22. /// associated parameter may be null.</value>
  23. public bool ReturnValue { get; }
  24. }
  25. /// <summary>
  26. /// Specifies that when a method returns <see cref="ReturnValue"/>,
  27. /// the parameter is not <see langword="null"/> even if the corresponding type allows it.
  28. /// </summary>
  29. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  30. internal sealed class NotNullWhenAttribute : Attribute
  31. {
  32. /// <summary>
  33. /// Initializes the attribute with the specified return value condition.
  34. /// </summary>
  35. /// <param name="returnValue">The return value condition. If the method returns this
  36. /// value, the associated parameter is not null.</param>
  37. public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
  38. /// <summary>
  39. /// Gets the return value condition.
  40. /// </summary>
  41. /// <value>The return value condition. If the method returns this value, the
  42. /// associated parameter is not null.</value>
  43. public bool ReturnValue { get; }
  44. }
  45. /// <summary>
  46. /// Specifies that an output may be <see langword="null"/> even if the corresponding type disallows it.
  47. /// </summary>
  48. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
  49. internal sealed class MaybeNullAttribute : Attribute
  50. {
  51. }
  52. /// <summary>
  53. /// Specifies that the method will not return if the associated Boolean parameter is passed the specified value.
  54. /// </summary>
  55. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  56. internal sealed class DoesNotReturnIfAttribute : Attribute
  57. {
  58. /// <summary>
  59. /// Initializes the attribute with the specified parameter value.
  60. /// </summary>
  61. /// <param name="parameterValue">
  62. /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
  63. /// the associated parameter matches this value.
  64. /// </param>
  65. public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
  66. /// <summary>
  67. /// Gets the condition parameter value.
  68. /// </summary>
  69. public bool ParameterValue { get; }
  70. }
  71. }
  72. #endif