Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, 17 October 2014
( Dynamic )

.NET4.0 new runtime on top of the CLR to support dynamic languages – DLR.. Ruby and Python.

Why Dynamic?

Static: C#, F#, C

Dynamic: JavaScript, Python,Ruby, PHP .. generally.. allow to modify code and types at runtime.  No compiler.  So only perform type checking at runtime (if at all)

 

When the compiler sees the dynamic keyword, it turns off compile time checks.

class Program { static void Main() { //Object o = GetASpeaker(); // Cooerce GetASpeaker as an Employee //Employee o = GetASpeaker() as Employee; //Use reflection.. if we know there is a Speak method //o.GetType().GetMethod("Speak").Invoke(o,null); //o.Speak(); dynamic o = GetASpeaker(); o.Speak(); } private static object GetASpeaker() { return new Employee { FirstName = "Scott" }; } } public class Employee { public string FirstName { get; set; } public void Speak() { Console.WriteLine("Hi, my name is {0}", FirstName); } }

It works.

Com Interop – Excel

image

Type excelType = Type.GetTypeFromProgID("Excel.Application"); dynamic excel = Activator.CreateInstance(excelType); excel.Visible = true; excel.Workbooks.Add(); dynamic sheet = excel.ActiveSheet; Process[] processes = Process.GetProcesses(); for (int i = 0; i < processes.Length; i++) { sheet.Cells[i + 1, "A"] = processes[i].ProcessName; sheet.Cells[i + 1, "B"] = processes[i].Threads.Count; }

Expando

dynamic expando = new ExpandoObject(); expando.Name = "Scott"; expando.Speak = new Action(() => Console.WriteLine(expando.Name)); //expando.Speak(); foreach (var member in expando) { Console.WriteLine(member.Key); }

prints Name, Speak

Parse an XML file:

// XDocument is in System.Xml.Linc var doc = XDocument.Load("Employees.xml"); foreach (var element in doc.Element("Employees") .Elements("Employee")) { Console.WriteLine(element.Element("FirstName").Value); } // Expando objects in expando var doc2 = XDocument.Load("Employees.xml").AsExpando(); foreach (var employee in doc2.Employees) { Console.WriteLine(employee.FirstName); }

then a simple extension method:

public static class ExpandoXml { public static dynamic AsExpando(this XDocument document) { return CreateExpando(document.Root); } private static dynamic CreateExpando(XElement element) { var result = new ExpandoObject() as IDictionary<string, object>; if (element.Elements().Any(e => e.HasElements)) { var list = new List<ExpandoObject>(); result.Add(element.Name.ToString(), list); foreach (var childElement in element.Elements()) { list.Add(CreateExpando(childElement)); } } else { foreach (var leafElement in element.Elements()) { result.Add(leafElement.Name.ToString(), leafElement.Value); } } return result; } }

CreateExpando is a recursive function that calls itself.

DynamicObject

A bit of work – but lots of power here.

| | # 
# 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.

| | # 
# Thursday, 21 July 2011
( BDD | Dynamic | WebMatrix )

WebMatix.Data

http://studiostyl.es/ – Obsidian

{ is not on a new line.

image

SELECT SCOPE_IDENTITY – brings back id of recently inserted record

 

Testing all done in razor

Get source code to see flashy nice stuff (messages on top)

Kind of like RSpec (BDD Tool for Ruby)

image

BDD stuff here:

http://blog.wekeroad.com/microsoft/the-super-dynamic-massive-freakshow

https://github.com/robconery/massive – ansyc (Task Parallel Lib) too..

http://blog.wekeroad.com/helpy-stuff/and-i-shall-call-it-massive

| | #