Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Thursday, 06 October 2011

Whilst getting my head around Rob Conery’s Massive ORM, I needed to figure out what the dynamic keyword is in C#4:

class Program {
        static void Main(string[] args) {
            Calculator calc = new Calculator();
            int sum = calc.Add(1, 2);
            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }

    public class Calculator {
        public int Add(int a, int b) {
            return a+b;
        }
    }

creation of an object, invokation of a method, and the collection of a return value

Var

Can represent any type that can be determined at compile time.

class Program {
        static void Main(string[] args) {
            //int
            var i = 5;
            //string
            var x = "hello";
            //int[]
            var y = new[] { 0, 1, 2, 3 };
            //anonymous type
            var anon = new { Name = "Dave", Age = 34 };
            //List<int>
            var list = new List<int>();
            //Calculator
            var calc = new Calculator();
            int sum = calc.Add(1, 2);
            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }

    public class Calculator {
        public int Add(int a, int b) {
            return a+b;
        }
    }

 

Dynamic

at runtime you get the type

class Program {
        static void Main(string[] args) {
            //int
            dynamic i = 5;
            //string
            dynamic x = "hello";
            //int[]
            dynamic y = new[] { 0, 2, 3, 4 };
            //anonymous type
            dynamic anon = new {Name="Dave", Age=34};

            Console.WriteLine(i + x);

            dynamic calc = new Calculator();
            //don't get intellisense when press .
            int sum = calc.Add(1, 2);
            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }

    public class Calculator {
        public int Add(int a, int b) {
            return a+b;
        }
    }

 

ExpandoObject and TryInvokeMember

An object whose members can be dynamically added and removed at runtime.

image

can’t do anything with the expandoObject though!

image

Much better.  Saying to the compiler – “Playing with a different set of rules”.

Why doesn’t Product need to by Dynamic?

           dynamic tbl = new DynamicModel("Northwind", "Products", "ProductID");
            //tbl.Single returns a dynamic of type ExpandoOnject, not an ExpandoObject.
            //acutally an IEnumrable<dynamic>.FirstOfDefault()
            var product = tbl.Single();

We don’t need product to be dynamic here as tbl.Single returns a dynamic of type Expando.

tbl has to be dynamic otherwise the TryInvokeMember wont work, as there is no method called Single.

| | #