Battle of the Code Generators

by Gigi Sayfan



Listing One

using System;

namespace EventDemo

{

   [EventInterface]

    public interface ISomeInterface

    {

        void OnThis(int x);

        void OnThat(string s);

        void OnTheOther(bool b, string s);

    };

    [EventInterface]

    public interface IAnotherInterface

    {

        void OnBim();

        void OnBam();

        void OnBom();

    };

}





Listing Two



// Auto-generated Switchboard class

using System.Collections;

using System.Diagnostics;

namespace EventDemo

{

    class Switchboard :

        EventDemo.ISomeInterface,

        EventDemo.IAnotherInterface

    {

        public static Switchboard Instance

        {

            get { return m_instance; }

        }

        public bool Subscribe(EventDemo.ISomeInterface sink)

        {

            if (m_someInterfaceList.Contains(sink))

            {

                Debug.Assert(false);

                return false;

            }

            m_someInterfaceList.Add(sink);

            return true;

        }

        public bool Unsubscribe(EventDemo.ISomeInterface sink)

        {

            if (!m_someInterfaceList.Contains(sink))

            {

                Debug.Assert(false);

                return false;

            }

            m_someInterfaceList.Remove(sink);

            return true;

        }

        public bool Subscribe(EventDemo.IAnotherInterface sink)

        {

            if (m_anotherInterfaceList.Contains(sink))

           {

                Debug.Assert(false);

                return false;

            }

            m_anotherInterfaceList.Add(sink);

            return true;

        }

        public bool Unsubscribe(EventDemo.IAnotherInterface sink)

        {

            if (!m_anotherInterfaceList.Contains(sink))

            {

                Debug.Assert(false);

                return false;

            }

            m_anotherInterfaceList.Remove(sink);

            return true;

        }

        public void OnThis(System.Int32 x)

        {

          foreach (EventDemo.ISomeInterface subscriber in m_someInterfaceList)

              subscriber.OnThis(x);

        }

        public void OnThat(System.String s)

        {

          foreach (EventDemo.ISomeInterface subscriber in m_someInterfaceList)

                subscriber.OnThat(s);

        }

        public void OnTheOther(System.Boolean b,System.String s)

        {

          foreach (EventDemo.ISomeInterface subscriber in m_someInterfaceList)

                subscriber.OnTheOther(b,s);

        }

        public void OnBim()

        {

          foreach (EventDemo.IAnotherInterface subscriber in 

                       m_anotherInterfaceList) subscriber.OnBim();

        }

        public void OnBam()

        {

          foreach (EventDemo.IAnotherInterface subscriber in 

                       m_anotherInterfaceList) subscriber.OnBam();

        }

        public void OnBom()

        {

            foreach (EventDemo.IAnotherInterface subscriber in 

                       m_anotherInterfaceList) subscriber.OnBom();

        }

        ArrayList m_someInterfaceList = new ArrayList();

        ArrayList m_anotherInterfaceList = new ArrayList();

        static Switchboard m_instance = new Switchboard();

    }

}



Listing Three



namespace EventDemo

using System;



{

    class EventHandler_1 : ISomeInterface

    {

        public void OnThis(int x)

        {

            Console.WriteLine("{0} received OnThis({1})", GetType().Name, x);

        }

        public void OnThat(string s)

        {

            Console.WriteLine("{0} received OnThat({1})", GetType().Name, s);

        }

        public void OnTheOther(bool b, string s)

        {

            Console.WriteLine("{0} received OnTheOther({1}, {2})", 

                                                       GetType().Name, b, s);

        }

    }

    class EventHandler_2 : IAnotherInterface

    {

        public void OnBim()

        {

            Console.WriteLine("{0} received OnBim()", GetType().Name);

        }

        public void OnBam()

        {

            Console.WriteLine("{0} received OnBam()", GetType().Name);

        }

        public void OnBom()

        {

            Console.WriteLine("{0} received OnBom()", GetType().Name);

        }

    }

    class EventHandler_3 : ISomeInterface, IAnotherInterface

    {

        public void OnThis(int x)

        {

            Console.WriteLine("{0} received OnThis({1})", GetType().Name, x);

        }

        public void OnThat(string s)

        {

            Console.WriteLine("{0} received OnThat({1})", GetType().Name, s);

        }

        public void OnTheOther(bool b, string s)

        {

            Console.WriteLine("{0} received OnTheOther({1}, {2})", 

                                                      GetType().Name, b, s);

        }

        public void OnBim()

        {

            Console.WriteLine("{0} received OnBim()", GetType().Name);

        }

        public void OnBam()

        {

           Console.WriteLine("{0} received OnBam()", GetType().Name);

        }

        public void OnBom()

        {

            Console.WriteLine("{0} received OnBom()", GetType().Name);

        }

    }

}





Listing Four



using System;

namespace EventDemo

{

    class MainClass

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main(string[] args)

        {

            Switchboard s = Switchboard.Instance;

            EventHandler_1 eh_1 = new EventHandler_1();

            EventHandler_2 eh_2 = new EventHandler_2();

            EventHandler_3 eh_3 = new EventHandler_3();

            s.Subscribe(eh_1 as ISomeInterface);

            s.Subscribe(eh_2 as IAnotherInterface);

            s.Subscribe(eh_3 as ISomeInterface);

            s.Subscribe(eh_3 as IAnotherInterface);

            s.OnThis(5);

            s.OnThat("Yeahh, it works!!!");

            s.OnTheOther(true, "Yeahh, it works!!!");

            s.OnBim();

            s.OnBam();

            s.OnBom();

            s.Unsubscribe(eh_1 as ISomeInterface);

            s.Unsubscribe(eh_2 as IAnotherInterface);

            s.Unsubscribe(eh_3 as ISomeInterface);

            s.Unsubscribe(eh_3 as IAnotherInterface);

        }

    }

}





Listing Five



using System;

namespace EventDemo

{

    [AttributeUsage( AttributeTargets.Interface, AllowMultiple = false)]

    public class EventInterfaceAttribute : System.Attribute

    {

   }

}





Listing Six



using System;

using System.Reflection;

namespace ReflectionDemo

{

    class AssemblyDumper

    {

        static public void Dump(Assembly a)

        {

            Type[] types = a.GetTypes();

            foreach (Type type in types)

                DumpType(type);

        }

        static private void DumpType(Type type)

        {

            Console.WriteLine(" ---- {0} ----", type.FullName); 

            MethodInfo[] methods = type.GetMethods();

            foreach (MethodInfo method in methods)

            {

                Console.Write("{0} {1}(", method.ReturnType, method.Name);

                ParameterInfo[] parameters = method.GetParameters();

                foreach (ParameterInfo parameter in parameters)

                {

                    string typeName = parameter.ParameterType.Name;

                    string parameterName =  parameter.Name;

                    Console.Write("{0} {1}", 

                           parameter.ParameterType.Name, parameter.Name);

                    if (parameter.IsOptional)

                        Console.Write(" = {0}", 

                                      parameter.DefaultValue.ToString());

                    if (parameter.Position < parameters.Length-1)

                        Console.Write(", ");

                }

                Console.WriteLine(")");

            }

        }

    }

}











5



