Code Monkey home page Code Monkey logo

eventbus's Issues

集合Autofac后ActionEventHandler如何触发?

圣杰兄请教一个问题下,我结合了Autofac实现了事件总线的注册和Trigger的解耦,但是唯独Action的注册和调用没法通过Autofac做到,后面尝试使用反射实现也没办法,原因是ActionEventHandler的构造函数就是一个Action,希望 圣杰兄帮忙指点下。这是我触发事件的那一段代码:

public void Trigger(TEventData eventData) where TEventData : IEventData
{
//从事件映射集合里获取匹配当前EventData(事件源数据)的Handler
List handlerTypes = _eventStore.GetHandlersForEvent(eventData.GetType()).ToList();
if (handlerTypes.Count <= 0) return;
//循环执行事件处理函数
foreach (var handlerType in handlerTypes)
{
var handlerInterface = handlerType.GetInterface("IEventHandler`1");
var eventHandler = ContainerManager.Current.Container.Resolve(handlerInterface);
if (eventHandler.GetType().FullName == handlerType.FullName)
{
var handler = eventHandler as IEventHandler;
handler?.HandleEvent(eventData);
}
else
{
//如果Ioc容器里没有则需要反射生成
//因为ActionEventHandler的构造函数需要有指定的入参:Action,这里没法反射!!
MethodInfo methodInfo = handlerType.GetMethod("HandleEvent").MakeGenericMethod();
if (methodInfo != null)
{
object obj = Activator.CreateInstance(handlerType);
methodInfo.Invoke(obj, new object[] { eventData });
}
}
}

    }

线程安全 字典集合

线程安全字典集合可以采用 类似这样的 ConcurrentDictionary<string, ConcurrentBag<ServiceResult>> 处理方式,,这样就可不用设置线程同步锁对象了

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.