Taylohtio/IDP/webapi/webapi.Infrastructure.Common/EventsHandlerFactory.cs

53 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using webapi.Domain.SeedWork;
using Autofac;
using System.Reflection;
namespace webapi.Domain.Events
{
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);
// mInfo = typeof(Program).GetMethod("MethodA",
//BindingFlags.Public | BindingFlags.Instance,
//null,
//CallingConventions.Any,
//new Type[] { typeof(int[]) },
//null););
m.Invoke(instance, new[] { (object)env });
//instance.(env);
//return instance1 as IEventHandler; // scope.Resolve<IEventHandler<T>>();
}
}
//public IEventHandler<TResult, T> GetHandler<TResult, T>(T evt) where T : INotification where TResult: Entity
//{
// return _container.Resolve<IEventHandler<TResult, T>>();
//}
}
}