Dynamic keyword in C#

In this blog post, I will try to understand the dynamic keyword that was introduced in C# 4.0. These notes are from Alexandra Rusina's article.
Usually dynamic languages such as JavaScript, Ruby, Python etc don't perform compile time checks therefore no compile time errors. C# and Java are considered to be statically typed languages that's why we get the compilation errors and intellisense. To enhance interoperability between dynamic and static languages, a keyword dynamic was added in C#. This keyword tells the compiler to turn off the compile time checking.
object work the same way, but to perform explicit type operations you have to explicitly cast the object to that specified type first.
var keyword also works similarly but the difference is that you can not change the type of var at runtime.
Reflection
Earlier to read objects from different systems, we had to rely on reflection to read the objects and its members and properties. This kind of code was tough to read. Dynamic makes it a bit easier.
Dynamic Method Bags
Dynamic method bags are objects that are created at run time and they can add or remove properties and methods.
Please see the following code which provides examples for the topics discussed above.
public class TV
    {
        public void Show(string str)
        {
            Console.WriteLine(str);
        }
    }

    public class A04DynamicKeyword
    {
        internal static void Run()
        {
            dynamic d = "sachin tendulkar";
            Console.WriteLine(d.GetType());
            
            // this will compile because its dynamic so type information is not known
            // but at runtime this will fail
            //d++;

            d = 100;
            Console.WriteLine(d.GetType());

            // this will work
            d++;
            Console.WriteLine(d);

            object obj = "sachin tendulkar";
            Console.WriteLine(obj.GetType());

            obj = 100;
            Console.WriteLine(obj.GetType());

            // for object explicit cast has to be used
            obj = (int) obj + 1;
            Console.WriteLine(obj);

            var v = 1000;
            Console.Write(v.GetType());

            v++;
            Console.WriteLine(v);

            // this will throw compiler error
            // we can not change the type of var once its fixed
            //v = "sachin tendulkar";

            //reflection was tough to code and not intuitive
            object tvObj = GetTV();
            Type type = tvObj.GetType();
            type.InvokeMember("Show", BindingFlags.InvokeMethod, null, tvObj, new object[] {"Show from Object"});
            
            // dynamic is easy and intuitive
            dynamic tv = GetTV();
            tv.Show("Dynamic Show");

            // Dynamic Class
            dynamic expandoObj = new ExpandoObject();
            expandoObj.FlyProperty = "This property is added on fly";
            expandoObj.SomeMethod = (Action) (() => Console.WriteLine(expandoObj.FlyProperty));
            expandoObj.SomeMethod();

        }

        private static TV GetTV()
        {
            return new TV();
        }
    }
And here are the results

No comments: