Taylohtio/IDP/parsers/parser-management-fee/common/EventsHandlerFactory.cs

37 lines
1.0 KiB
C#

using Autofac;
using taloyhtio.idp.parser.common.domain;
using System;
using System.Reflection;
namespace taloyhtio.idp.parser.common
{
public class EventsHandlerFactory
{
readonly ILifetimeScope _container;
public EventsHandlerFactory(ILifetimeScope container)
{
_container = container;
}
public void ResolveAndRun<T>(T env) where T : INotification
{
using (var scope = _container.BeginLifetimeScope())
{
var instance = scope.Resolve(typeof(BaseEventHandler<>)
.MakeGenericType(env.GetType()));// as BaseEventHandler<>; // IEventHandler;
Type t = instance.GetType();
var m = t.GetMethod("Handle",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
new Type[] { env.GetType() },
null);
m.Invoke(instance, new[] { (object)env });
}
}
}
}