diff --git a/src/Mapster/TypeAdapter.cs b/src/Mapster/TypeAdapter.cs index 2e1db2f1..b9eba4aa 100644 --- a/src/Mapster/TypeAdapter.cs +++ b/src/Mapster/TypeAdapter.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace Mapster @@ -21,7 +22,8 @@ public static ITypeAdapterBuilder BuildAdapter(this TSource so /// Destination type. /// Source object to adapt. /// Adapted destination type. - public static TDestination Adapt(this object? source) + [return: NotNullIfNotNull(nameof(source))] + public static TDestination? Adapt(this object? source) { return Adapt(source, TypeAdapterConfig.GlobalSettings); } @@ -33,14 +35,15 @@ public static TDestination Adapt(this object? source) /// Source object to adapt. /// Configuration /// Adapted destination type. - public static TDestination Adapt(this object? source, TypeAdapterConfig config) + [return: NotNullIfNotNull(nameof(source))] + public static TDestination? Adapt(this object? source, TypeAdapterConfig config) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (source == null) - return default!; + return default; var type = source.GetType(); var fn = config.GetDynamicMapFunction(type); - return fn(source); + return fn(source)!; } ///