/// <summary>Checks whether a type is nullable.</summary> /// <param name="contextualType">The type.</param> /// <param name="defaultReferenceTypeNullHandling">The default reference type null handling used when no nullability information is available.</param> /// <returns>true if the type can be null.</returns> public virtual bool IsNullable(ContextualType contextualType, ReferenceTypeNullHandling defaultReferenceTypeNullHandling) { var jsonPropertyAttribute = contextualType.GetContextAttribute <JsonPropertyAttribute>(); if (jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.DisallowNull) { return(false); } if (contextualType.ContextAttributes.FirstAssignableToTypeNameOrDefault("NotNullAttribute", TypeNameStyle.Name) != null) { return(false); } if (contextualType.ContextAttributes.FirstAssignableToTypeNameOrDefault("CanBeNullAttribute", TypeNameStyle.Name) != null) { return(true); } if (contextualType.Nullability != Nullability.Unknown) { return(contextualType.Nullability == Nullability.Nullable); } var isValueType = contextualType.Type != typeof(string) && contextualType.TypeInfo.IsValueType; return(isValueType == false && defaultReferenceTypeNullHandling != ReferenceTypeNullHandling.NotNull); }
public override bool IsNullable(ContextualType contextualType, ReferenceTypeNullHandling defaultReferenceTypeNullHandling) { if (contextualType.GetContextAttribute <AlwaysPresentAttribute>() != null) { return(false); } return(base.IsNullable(contextualType, defaultReferenceTypeNullHandling)); }
/// <summary> /// Extracts the tuple element names if provided. /// </summary> /// <param name="contextualType">The contextual type for a ValueTuple type.</param> /// <returns>Returns the tuple element names or null.</returns> public static IList <String?>?GetTupleElementNames(this ContextualType contextualType) => contextualType.GetContextAttribute <TupleElementNamesAttribute>()?.TransformNames;