是否可以解决此警告:
将空文本或可能的空值转换为非空类型。
而不对此C#代码进行抑制
List<PropertyInfo> sourceProperties = sourceObject.GetType().GetProperties().ToList<PropertyInfo>();
List<PropertyInfo> destinationProperties = destinationObject.GetType().GetProperties().ToList<PropertyInfo>();
foreach (PropertyInfo sourceProperty in sourceProperties)
{
if (!Equals(destinationProperties, null))
{
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
}它使用反射。

我正在使用VisualStudio2019和.NET Core3.1。
发布于 2020-07-14 16:56:02
如果找不到要查找的内容,Find()可以返回null。因此,destinationProperty可以变为null。
因此,解决方案是将其声明为可空:
PropertyInfo? destinationProperty = ...或者抛出一个异常:
PropertyInfo destinationProperty = ...Find() ?? throw new ArgumentException(...)https://stackoverflow.com/questions/62899915
复制相似问题