1 回答
TA贡献1864条经验 获得超2个赞
我终于设法解决了这个问题。所以这是万一有人经历过像我一样的事情的答案:
private void OnNotifyAgentEntityChanged(object sender, EntityChangedEventArgs entityChangedEventArgs)
{
this.PublishEvent((TEntity)entityChangedEventArgs.Entity);
}
private void PublishEvent(TEntity entity)
{
object args = this.GetEventArgs(entity);
object eventObject = this.GetEventObject(entity);
MethodInfo publishMethod = eventObject.GetType().GetMethod("Publish");
publishMethod.Invoke(eventObject, new[] { args });
}
private object GetEventArgs(TEntity entity)
{
Type viewModelIdentifierEventArgsType = typeof(ViewModelIdentifierEventArgs<>).MakeGenericType(entity.GetType());
object args = Activator.CreateInstance(viewModelIdentifierEventArgsType, Guid.NewGuid(), entity);
return args;
}
private object GetEventObject(TEntity entity)
{
MethodInfo getEventMethod = typeof(IEventAggregator).GetMethod("GetEvent");
Type entityModifiedEventType = typeof(EntityModifiedEvent<>).MakeGenericType(entity.GetType());
MethodInfo genericGetEventMethod = getEventMethod.MakeGenericMethod(entityModifiedEventType);
object eventObject = genericGetEventMethod.Invoke(this.eventAggregator, null);
return eventObject;
}
通过这样做,EntityModifiedEvent<>无论TEntity. 我只需要添加KnownType我的,EntityChangedEventArgs以便它可以被反/序列化
- 1 回答
- 0 关注
- 221 浏览
添加回答
举报