【发布时间】:2020-04-16 20:15:05
【问题描述】:
我将 AutoMapper 用于 A 和 B 类。该项目是一个 .Net 核心项目,<Nullable>enabled</Nullable>。
public class A
{
public ClassX? X { get; set; } // ClassX is a custom class with the property of Value.
//....
}
public class B
{
public string? X { get; set; }
// ....
}
在下面的映射代码中
public void Mapping(Profile profile)
{
// ....
_ = profile?.CreateMap<A, B>()
.ForMember(d => d.X, o => o.MapFrom(s => s.X.Value)); // warning on s.X
}
它在s.X 上有以下警告:
警告 CS8602 取消引用可能为空的引用。
如何在不使用#pragma warning disable CS8602的情况下摆脱警告?
我尝试使用 Null 条件将 o.MapFrom(s => s.X.Value) 更改为 o.MapFrom(s => s.X?.Value)。但它在s.X?.Value 上出现以下错误。
错误 CS8072 表达式树 lambda 可能不包含空传播运算符
【问题讨论】:
-
我已经删除了 CS8602,这个警告它的可怕甚至特定的警卫(例如 Guard/Ensure fail early)都被忽略了,而且需要大量的条件,因为它无法分辨。然后它会导致代码被污染/更难阅读并且人们浪费时间。
标签: c# automapper