Learn.Develop.Share

Twitter












dynamic - Introduction

Lets examine the dynamic key word introduced in .Net 4.0. It can keep an instance of any type. Then what is the difference between it and System.Object. Well the essential difference between them is the dynamic method dispatching which enables lazy binding, i.e. it allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the normal static binding of C# and instead gets resolved dynamically.
It provides a unified approach to selecting opeartions dynamically.
 
Lets see an example:
 
//Class Program.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coderslog.Net4.Samples
{
    class Program
    {
        private static Type _toggle;

        static void Main(string[] args)
        {
            var val = "";
            do
            {
                                 //Dynamic way
                dynamic d = GetMeMyDynamicObject();
                Console.WriteLine(d.GetType().FullName);
                d.DoSometing();
                                 
                                //Reflection way
                object inst = GetMeMyDynamicObject();
                Console.WriteLine(inst.GetType().FullName);
                Invoke(inst, "DoSometing" );       
         
                
                                 Console.WriteLine(
                                             "Press Q/q to quit and y/Y to continue!");
                val = Console.ReadLine();

            } while (val.ToLower() != "q");
        }

        
private static void Invoke(object inst, string methodName)
        {
            var methodInfo = inst.GetType().GetMethod(methodName);
            methodInfo.Invoke(inst, null);
        }


        private static dynamic GetMeMyDynamicObject()
        {
            if (_toggle == null)
                _toggle = typeof(BehaviourB);
            if (typeof(BehaviourB) == _toggle)
            {
                _toggle = typeof(BehaviourA);
                return new BehaviourA();
            }
            else
            {
                _toggle = typeof(BehaviourB);
                return new BehaviourB();
            }
        }
    }
}
 
//Class BehaviourA.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coderslog.Net4.Samples
{
    public class BehaviourA
    {
        public void DoSometing()
        {
            Console.WriteLine("Behaviour A - Do Something!");
        }
    }
}
 
//Class BehaviourB.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Coderslog.Net4.Samples
{
    public class BehaviourB
    {
        public void DoSometing()
        {
            Console.WriteLine("Behaviour B - Do Something!");
        }
    }
}
 
Notice the dynamic way of invoking the method and Reflection way. Thats the benefit of dyanmic and many more. But it comes with its own problem. The same problem that is there with the reflection way is still there.
 
We will as well look at some other aspects of the dynamic in the following posts.


Feedback

No comments posted yet.