在“ ProDinner ”项目中,ValueInjecter用于映射。我使用最新版本,在实体和int[]之间转换时将ConventionInjection替换为LoopInjection,我得到了EntitiesToInts类的代码:
public class EntitiesToInts : LoopInjection
{
protected override bool MatchTypes(Type src, Type trg)
{
return trg == typeof(int[])
&& src.IsGenericType
&& src.GetGenericTypeDefinition() == typeof(ICollection<>)
&& src.GetGenericArguments()[0].IsSubclassOf(typeof(Entity));
}
protected override void SetValue(object source, object target, PropertyInfo sp, PropertyInfo tp)
{
var val = sp.GetValue(source);
if (val != null)
{
tp.SetValue(target, (val as IEnumerable<Entity>).Select(o => o.Id).ToArray());
}
}
}
如何完成 IntsToEntities 类?