dimanche 29 mars 2020

Invoke non-generic method from generic method

So, i have handler class, which derives from a generic abstract class.

 public class CreateMovieEventHandler : EventHandler<CreateMovieCommand>
    {
        public override void Handle(CreateMovieCommand command)
        {
            throw new System.NotImplementedException();
        }
    }

I also have a notifier class which is responsible to call handle method by TCommand.

public class EventNotifier
    {
        public void Notify<TCommand>(TCommand command)
        {
            InvokeHandler<TCommand>(command);
        }

        protected void InvokeHandler<TCommand>(TCommand command)
        {
            try
            {
                Type type = Assembly.GetExecutingAssembly().GetTypes()
                           .Where(t => t.IsSubclassOf(typeof(EventHandler<TCommand>))).FirstOrDefault();
                if (type == null)
                    return;

                MethodInfo method = type.GetMethod("Handle");

            }
            catch(Exception exception){}
        }
    }

In InvokeHandler method I am trying to invoke CreateMovieEventHandler's Handle method... any suggestion...





Aucun commentaire:

Enregistrer un commentaire