我正在使用 ValueInjecter 映射两个相同的对象。我遇到的问题是 ValueInjector 从我的源复制空值到我的目标。所以我将大量数据丢失为空值。
这是我的对象的一个示例,有时只填充了一半,这导致它的空值覆盖了目标对象。
public class MyObject()
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<OtherObject> OtherObjects { get; set; }
}
to.InjectFrom(from);
最佳答案
对于那些使用 ValueInjecter
v3+ 的用户,ConventionInjection
已被弃用。使用以下实现相同的结果:
public class NoNullsInjection : LoopInjection
{
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
{
if (sp.GetValue(source) == null) return;
base.SetValue(source, target, sp, tp);
}
}
用法:
target.InjectFrom<NoNullsInjection>(source);
关于c# - 如何阻止 ValueInjecter 映射空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10608978/