我试图在 OnPostUpdate 事件中获取更新实体的旧状态。@event 参数的 OldState 属性将对象作为数组返回,我需要它作为对象。我尝试了以下方法:
public class EventListener : IPostUpdateEventListener {
public void OnPostUpdate(PostUpdateEvent @event) {
// Get the property names to align with the indexes of the old state
var propertyNames = @event.Persister.PropertyNames;
// Get the original entity values
var type = @event.Entity.GetType();
var original = Activator.CreateInstance(type);
// Set the values
for (var i = 0; i < @event.OldState.Length; i++) {
// Get the property and value
var property = type.GetProperty(@event.Persister.PropertyNames[i]);
var value = @event.OldState[i];
if (property.CanWrite && !(@event.Persister.PropertyTypes[i] is CollectionType)) {
if (value != null && (@event.Persister.PropertyTypes[i] is ComponentType || @event.Persister.PropertyTypes[i] is EntityType))
property.SetValue(original, CloneObject(value));
else
property.SetValue(original, value);
}
}
}
这是 CloneObject 方法:
private object CloneObject(object source) {
var sourceType = source.GetType();
var target = Activator.CreateInstance(sourceType, true);
foreach (var property in type.GetProperties()) {
var value = property.GetValue(source, null);
if (property.CanWrite && (property.PropertyType == typeof(string) || !typeof(IEnumerable).IsAssignableFrom(property.PropertyType)) && !property.PropertyType.IsInterface) {
if (value != null && typeof(IEntity).IsAssignableFrom(property.PropertyType))
property.SetValue(target, CloneObject(value));
else
property.SetValue(target, value);
}
}
return target;
}
我做的第一件事是遍历原始对象中的属性,然后尝试处理组件和引用类型(忽略列表)。这会正确复制对象。但是,它随后会引发错误:
集合 [Models.ContentType.Tags] 未由 flush() 处理
编辑: ContentType 类是我正在更新的属性之一的类型。这是它的实现,因为它有帮助:
public class ContentType : Entity<int> {
public virtual string Name { get; set; }
public virtual IList<ContentTag> Tags { get; set; }
public ContentType() {
Tags = new List<ContentTag>();
}
}
当我尝试获取其他实体的旧状态时,我也会遇到类似的错误。如果有人能告诉我正确的方法,我将不胜感激。谢谢