是否可能解决此警告:
将 null 字面量或可能为 null 的值转换为非可空类型。
而不需要抑制此 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.
}
}
使用反射的代码。
我正在使用 Visual Studio 2019 和 .NET Core 3.1。
PropertyInfo destinationProperty
没有?
),但Find()
方法可能返回 null 值。这是 C# 8 的一个特性。 - CodeCaster