通用发布事件类

时间:2018-07-24 11:51:59

标签: c# wpf

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

在运行时,我不知道会有什么实体,这就是为什么我决定使其通用。问题是,可以存在的每个单一类型都从基类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,而我需要将其设为AnimalTree类型,就像我一样表示从EntityBase

继承

这是可行的,但我需要将Tree的类型更改为来自EntityChangedEventArgs.Entity的任何类型:

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

        IEntityModifiedEventFactory<Tree> entityModifiedEventFactory = new EntityModifiedEventFactory<Tree> ((Tree)entityChangedEventArgs.Entity, this.eventAggregator);
        EntityModifiedEvent<Tree> entityModifiedEvent = entityModifiedEventFactory.Create();

        entityModifiedEvent.Publish(eventArgs);

1 个答案:

答案 0 :(得分:1)

我终于设法解决了这个问题。所以这是万一有人经历像我一样的事情的答案:

    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,以便可以对其进行反序列化/序列化