猿问

通用发布事件类

我正在尝试开发一个通用类来引发事件。


在运行时,我不知道那里会出现什么实体,这就是我决定将其设为通用的原因。问题是,可以存在的每个类型都继承自基类EntityBase


在这个类中,我返回(或者我想 :P )具有正确类型的事件。


public class EntityModifiedEventFactory<TEntity> : IEntityModifiedEventFactory<TEntity> where TEntity : EntityBase

{

    private readonly IEventAggregator eventAggregator;


    public EntityModifiedEventFactory(IEventAggregator eventAggregator)

      {

         ParameterChecker.IsNotNull(eventAggregator, "eventAggregator");


         this.eventAggregator = eventAggregator;

      }


    public EntityModifiedEvent<TEntity> Create()

      {

         return this.eventAggregator.GetEvent<EntityModifiedEvent<TEntity>>();

      }

}

这里我调用构造函数然后发布事件(不编译)


private void OnNotifyAgentEntityChanged(object sender, EntityChangedEventArgs entityChangedEventArgs)

{

    ViewModelIdentifierEventArgs<entityChangedEventArgs.Entity.GetType()> eventArgs = new ViewModelIdentifierEventArgs<entityChangedEventArgs.Entity.GetType() > (entityChangedEventArgs.ViewModelIdentifier, (entityChangedEventArgs.Entity.GetType())entityChangedEventArgs.Entity);


    IEntityModifiedEventFactory<entityChangedEventArgs.Entity.GetType() > entityModifiedEventFactory = new EntityModifiedEventFactory<entityChangedEventArgs.Entity.GetType()> ((entityChangedEventArgs.Entity.GetType())entityChangedEventArgs.Entity, this.eventAggregator);

    EntityModifiedEvent<entityChangedEventArgs.Entity.GetType()> entityModifiedEvent = entityModifiedEventFactory.Create();


    entityModifiedEvent.Publish(eventArgs);

}

我应该如何编写特定类型来构建我的EntityModifiedEventFactory? . 我试过这个,entityChangedEventArgs.Entity.GetType()但我没有办法让它工作。


如果我离开TEntity的IEntityModifiedEventFactory<TEntity> entityModifiedEventFactory...将是一个EntityBase和我需要它是一个Animal或一个Tree类型的就像我说的继承EntityBas

米脂
浏览 222回答 1
1回答

慕斯王

我终于设法解决了这个问题。所以这是万一有人经历过像我一样的事情的答案:&nbsp; &nbsp; private void OnNotifyAgentEntityChanged(object sender, EntityChangedEventArgs entityChangedEventArgs)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.PublishEvent((TEntity)entityChangedEventArgs.Entity);&nbsp; &nbsp; }&nbsp; &nbsp; private void PublishEvent(TEntity entity)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; object args = this.GetEventArgs(entity);&nbsp; &nbsp; &nbsp; &nbsp; object eventObject = this.GetEventObject(entity);&nbsp; &nbsp; &nbsp; &nbsp; MethodInfo publishMethod = eventObject.GetType().GetMethod("Publish");&nbsp; &nbsp; &nbsp; &nbsp; publishMethod.Invoke(eventObject, new[] { args });&nbsp; &nbsp; }&nbsp; &nbsp; private object GetEventArgs(TEntity entity)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Type viewModelIdentifierEventArgsType = typeof(ViewModelIdentifierEventArgs<>).MakeGenericType(entity.GetType());&nbsp; &nbsp; &nbsp; &nbsp; object args = Activator.CreateInstance(viewModelIdentifierEventArgsType, Guid.NewGuid(), entity);&nbsp; &nbsp; &nbsp; &nbsp; return args;&nbsp; &nbsp; }&nbsp; &nbsp; private object GetEventObject(TEntity entity)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MethodInfo getEventMethod = typeof(IEventAggregator).GetMethod("GetEvent");&nbsp; &nbsp; &nbsp; &nbsp; Type entityModifiedEventType = typeof(EntityModifiedEvent<>).MakeGenericType(entity.GetType());&nbsp; &nbsp; &nbsp; &nbsp; MethodInfo genericGetEventMethod = getEventMethod.MakeGenericMethod(entityModifiedEventType);&nbsp; &nbsp; &nbsp; &nbsp; object eventObject = genericGetEventMethod.Invoke(this.eventAggregator, null);&nbsp; &nbsp; &nbsp; &nbsp; return eventObject;&nbsp; &nbsp; }通过这样做,EntityModifiedEvent<>无论TEntity. 我只需要添加KnownType我的,EntityChangedEventArgs以便它可以被反/序列化
随时随地看视频慕课网APP
我要回答