Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Thanks

BTW, 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.
Posted
Updated 6-Mar-18 8:45am
v2

1 solution

Here's one way - simply call the Process method in each dispatcher.

C#
public class Dispatcher1
{
    public Dispatcher1()
    {
    }

    public bool Process(object data)
    {
        if (data is Object1)
        {
            ProcessThisObject((Object1)data);
            return true;
        }
        return false;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900