Example #1
0
        /*
         * 1.如何精简步骤
         * 2.如何解除 发布 与 订阅方的依赖 :
         * 3.如何避免在订阅者中同时处理多个事件逻辑
         */
        static void Main(string[] args)
        {
            FishingRod fishingRod = new FishingRod();
            FishingMan jeff       = new FishingMan("周凯凯");

            jeff.FishingRod = fishingRod;
            while (jeff.FishCount < 5)
            {
                jeff.Fishing();
                Console.WriteLine("---------------------");
            }
            Console.ReadKey();
        }
Example #2
0
        //public delegate void FishingHandler(FishingEventData eventData);

        //public event FishingHandler FishingEvent;


        //public FishingRod()
        //{
        //    //Assembly assembly = Assembly.GetExecutingAssembly();
        //    //foreach (Type type in assembly.GetTypes())
        //    //{
        //    //    if (!type.IsInterface && typeof(IEventHandler<FishingEventData>).IsAssignableFrom(type))
        //    //    {
        //    //        var dd = type.GetInterfaces();
        //    //        Type handlerInterface = type.GetInterface("IEventHandler`1");//获取类实现的泛型接口
        //    //        Type eventDataType = handlerInterface.GetGenericArguments()[0];//获取泛型参数
        //    //        if (eventDataType.Equals(typeof(FishingEventData)))
        //    //        {
        //    //            IEventHandler<FishingEventData> handler = Activator.CreateInstance(type) as IEventHandler<FishingEventData>;
        //    //            FishingEvent += handler.HandleEvent;
        //    //        }
        //    //    }
        //    //}
        //}

        public void ThrowHook(FishingMan man)
        {
            Console.WriteLine("开始下钩!");
            if (new Random().Next() % 2 == 0)
            {
                var type = (FishType) new Random().Next(0, 5);
                Console.WriteLine("铃铛:叮叮叮,鱼儿咬钩了");
                FishingEventData eventData = new FishingEventData();
                eventData.EventSource = this;
                eventData.FishType    = type;
                eventData.FishingMan  = man;
                EventBus.Default.Trigger <FishingEventData>(eventData);
            }
        }