Skip to content

LateApexEarlySpeed.Nullability.Generic

LateApexEarlySpeed edited this page Oct 4, 2024 · 1 revision

Intention to implement this library is to solve issue when reading generic type related nullability info.

Calling entrypoint is static method NullabilityType.GetType() which has 3 overloads:

NullabilityType GetType(Type type) // when type itself is not generic type
NullabilityType GetType(Type type, params NullabilityState[] genericTypeArgumentsNullabilities) // when type is generic type is generic type and its generic type arguments are not generic types
NullabilityType GetType(Type type, params NullabilityElement[] genericTypeArgumentsNullabilities) // when type is generic type and its generic type arguments are also generic type

Navigating apis of NullabilityType are defined align with System.Type, other memberInfo types (NullabilityPropertyInfo, NullabilityFieldInfo, NullabilityMethodInfo and NullabilityParameterInfo) inherited from their standard member types. Every Nullability related classes have corresponding properties to escape nullability type world back to runtime ones, like:

Type NullabilityType.Type { get; }

To get NullabilityType related nullability states:

class NullabilityType
{
  /// <summary>
  /// Get the annotated nullability state of current type
  /// </summary>
  NullabilityState NullabilityState { get; }
  /// <summary>
  /// Gets an array of the generic type arguments for this type.
  /// </summary>
  NullabilityType[] GenericTypeArguments { get; }
  /// <summary>
  /// returns the <see cref="NullabilityType"/> of the elements in current array, or null if the current Type is not an array
  /// </summary>
  NullabilityType? GetArrayElementType();
}

To get property related nullability info:

class NullabilityPropertyInfo : PropertyInfo
{
    /// <summary>
    /// Gets the nullability type of current property.
    /// </summary>
    NullabilityType NullabilityPropertyType { get; }
    /// <summary>
    /// Gets the nullability read state of current property.
    /// </summary>
    NullabilityState NullabilityReadState { get; }
    /// <summary>
    /// Gets the nullability write state of current property.
    /// </summary>
    NullabilityState NullabilityWriteState { get; }
}

To get field related nullability info:

class NullabilityFieldInfo : FieldInfo
{
    /// <summary>
    /// Gets the nullability state of current field.
    /// </summary>
    NullabilityState NullabilityState { get; }
    /// <summary>
    /// Gets the nullability type of current field.
    /// </summary>
    NullabilityType NullabilityFieldType { get; }
}

To get method parameters and return value related nullability info:

class NullabilityMethodInfo : MethodInfo
{
    /// <summary>
    /// Gets a <see cref="NullabilityParameterInfo"/> object that contains info about the return type of the method, including nullability.
    /// </summary>
    NullabilityParameterInfo NullabilityReturnParameter { get; }
    /// <summary>
    /// gets the parameters of the specified method or constructor.
    /// </summary>
    /// <returns>An array of type <see cref="NullabilityParameterInfo"/> containing information (including nullability) that matches the signature of the method (or constructor).</returns>
    NullabilityParameterInfo[] GetNullabilityParameters();
}

class NullabilityParameterInfo : ParameterInfo
{
    /// <summary>
    /// Gets the nullability state of current parameter.
    /// </summary>
    NullabilityState NullabilityState { get; }
    /// <summary>
    /// Gets the nullability type of current parameter.
    /// </summary>
    NullabilityType NullabilityParameterType { get; ]
}