Пример #1
0
        static void Main(string[] args)
        {
            MessageDisplay m = new MessageDisplay();

            m.DisplayMessage("Hello world");

            dynamic d = new MessageDisplay();

            d.Banana("hello world");
        }
Пример #2
0
        private static void BadDynamicCodeExample()
        {
            /* The class MessageDisplay contains a single method, called DisplayMessage.
             * The variable m is set to refer to an instance of this class, and the program calls the DisplayMessage
             * method on this reference. */
            MessageDisplay m = new MessageDisplay();

            m.DisplayMessage("Hello World");

            /* The variable d is declared as dynamic and set to refer to a MessageDisplay instance. The program
             * then contains a call of a method called Banana on the variable d. Normally this would not compile,
             * because the compiler can see that this method is not present in the class. Because the variable d
             * has been declared as dynamic, however, the program will compile with no errors, but when the program
             * is executed and exception will be generated when the Banana method is called. */
            dynamic d = new MessageDisplay();

            d.Banana("Hello World");
        }