views:

1019

answers:

5

Here is new C# future in version 4.0 known as dynamic. Show me the way i can use it in my code and how this future can help me?

+3  A: 

Two related questions:

bruno conde
Hope you don't mind my edit, but I thought it was a waste to post another answer and I didn't want it hidden in a comment
Sam Hasler
+1  A: 

One of the usages is interop between static and dynamic languages.

Say you want to invoke a javascript funtion fron silverlight:

HtmlPage.Window.Invoke("HelloWorldFunction");

If the window was dynamic (and properly implemented) you would be able to use it like this:

HtmlPage.Window.HelloWorldFunction();
Cristian Libardo
+3  A: 

Anders Hejlsberg did a nice wee PDC session called "The Future of C#". there's a pretty good demo of the use of the dynamic keyword:

http://channel9.msdn.com/pdc2008/TL16/

Kev
+3  A: 

Once you've a dynamic object, the compiler is least bothered about any method calls you might make on the dynamic object. The calls will be resolved only at the runtime. In this case, the method Read() is dispatched dynamically during run time.

What is more beautiful is, C# now gives you the flexibility to specify how the dynamic calls should be dispatched. You can implement the IDynamicObject, to write these binders yourself. For example, see how I'm creating a dynamic reader class, which allows you to call your own methods on an instance of that.

public class DynamicReader : IDynamicObject
    {
        public MetaObject GetMetaObject
              (System.Linq.Expressions.Expression parameter)
        {
            return new DynamicReaderDispatch (parameter);
        }
    }

    public class DynamicReaderDispatch : MetaObject
    {
        public DynamicReaderDispatch (Expression parameter) 
                   : base(parameter, Restrictions.Empty){ }

        public override MetaObject Call(CallAction action, MetaObject[] args)
        {
            //You might implement logic for dynamic method calls. Action.name
            // will give you the method name

            Console.WriteLine("Logic to dispatch Method '{0}'", action.Name);
            return this;
        }
    }

Now, the dynamic keyword can be used to create dynamic objects, much like

dynamic reader=new DynamicReader();
dynamic data=reader.Read();
amazedsaint
+2  A: 

We use the C# "dynamic" keyword with TDD.

This code doesn't compile because the method "Addition" is not implemented

[TestMethod()]
public void CalculatorThingAdd_2PositiveNumbers_ResultAdded()
{
    CalculatorThing myCalculator = new CalculatorThing();
    int result = 0; 
    int expcected = 3;

    // --> CalculatorThing  does not contain a definition for 'Addition'
    result = myCalculator.Addition(1, 2);

    Assert.AreEqual(result, expcected);
}

With the "dynamic" keyword the code compiles and the test fails! --> TDD

See answer here http://stackoverflow.com/questions/244302/what-do-you-think-of-the-new-c-4-0-dynamic-keyword/2243818#2243818

Peter Gfader