本文介绍了动态多重调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好! :) 问题是关于动态调度对象。这是一个会崩溃的代码: interface IDispatcher { void Dispatch(dynamic obj); } class MyClass1 {} class MyClass2 {} class Dispatcher1:IDispatcher { public void Dispatch(dynamic obj) { Console.WriteLine(Dispatcher.Dispatch dynamic); Dispatch(obj); } public void Dispatch(MyClass1 cl) { Console.WriteLine(Dispatcher.Dispatch MyClass1); } } class Dispatcher2:IDispatcher { public void Dispatch(dynamic obj) { Console.WriteLine (Dispatcher.Dispatch dynamic); Dispatch(obj); } public void Dispatch(MyClass2 cl) { Console.WriteLine(Dispatcher.Dispatch MyClass2); } } class Dispatchers { public void Add(IDispatcher disp) { _list.Add(disp ); } public void DispatchAll(object data) { _list.ForEach(x => x.Dispatch(data)); } readonly List< IDispatcher> _list = new List< IDispatcher>(); } class Program { static void Main(string [] args) { try { Dispatchers disps = new Dispatchers(); disps.Add(new Dispatcher1()); disps.Add(new Dispatcher2()); MyClass2 cl = new MyClass2(); disps.DispatchAll(cl); } catch(exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } } 我的尝试: 因此,正如您从上面所看到的,我正在向一个集合添加调度程序,该集合必须处理一些对象。整个想法当然是能够添加新的调度程序以及特定的调度程序不会知道每个对象 - 所以每个调度程序都知道,比如说,一个对象类型。当新对象到达时,它们会传递给Dispatchers集合,如果它不知道新对象,这个集合不会崩溃。 当前这段代码当然会崩溃,因为第一个调度程序不知道提供的对象(它将在第二个调度程序中调度)因此他将在循环中调用Dispatch(动态对象),直到它与StackOverflowException崩溃。 所以问题是,我必须做什么才能在调度程序中停止调度循环,调度程序没有特定对象类型的处理程序? 谢谢 BTW,我知道我可以添加一些标志和整数来增加并检查调用Dispatch方法的次数或从基类继承MyClass并提供Dispatch(BaseClass)方法,这将在此处停止。 但我正在寻找一个文明的解决方案,就像这个代码将成为API的一部分,它将提供给开发人员开发她的代码。解决方案 Hello everyone! :)The question is about dynamic dispatching of an object. Here is a code, which will crash:interface IDispatcher { void Dispatch(dynamic obj); } class MyClass1 { } class MyClass2 { } class Dispatcher1 : IDispatcher { public void Dispatch(dynamic obj) { Console.WriteLine("Dispatcher.Dispatch dynamic"); Dispatch(obj); } public void Dispatch(MyClass1 cl) { Console.WriteLine("Dispatcher.Dispatch MyClass1"); } } class Dispatcher2 : IDispatcher { public void Dispatch(dynamic obj) { Console.WriteLine("Dispatcher.Dispatch dynamic"); Dispatch(obj); } public void Dispatch(MyClass2 cl) { Console.WriteLine("Dispatcher.Dispatch MyClass2"); } } class Dispatchers { public void Add(IDispatcher disp) { _list.Add(disp); } public void DispatchAll(object data) { _list.ForEach(x => x.Dispatch(data)); } readonly List<IDispatcher> _list = new List<IDispatcher>(); } class Program { static void Main(string[] args) { try { Dispatchers disps = new Dispatchers(); disps.Add(new Dispatcher1()); disps.Add(new Dispatcher2()); MyClass2 cl = new MyClass2(); disps.DispatchAll(cl); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } }What I have tried:So, as you can see from the above, I am adding dispatchers to a collection, which has to process some object. The whole idea is of course to be able to add new dispatchers as well as a particular dispatcher will not know of every object - so every dispatcher knows about, lets say, one object type. While new objects arrive, they passed to Dispatchers collection, and this collection should not crash if it does not know new objects.Currently this code will crash of course, because first dispatcher does not know about provided object (which is going to be dispatched in second dispatcher) and so he is going to call Dispatch(dynamic object) in a loop until it is going to crash with StackOverflowException.So the question is, what I have to do to "stop" dispatching loop in a dispatcher, which does not have a handler for particular object type?ThanksBTW, I know that I can add some flags and integers to increment and check how many times Dispatch method has been called or inherit MyClass from base class and provide Dispatch(BaseClass) method, which will stop here.But I am looking for a "civilized" solution, like this code is going to be a part of an API, which will be provided to developers to develop further code. 解决方案 这篇关于动态多重调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 00:05