Vokinneberg
Vokinneberg

Reputation: 2017

Show me the way to use new "dynamic" keyword in C# 4.0

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?


Related questions:

Upvotes: 4

Views: 3222

Answers (4)

Cristian Libardo
Cristian Libardo

Reputation: 9258

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

Say you want to invoke a JavaScript function 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();

Upvotes: 2

Peter Gfader
Peter Gfader

Reputation: 7751

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 https://stackoverflow.com/questions/244302/what-do-you-think-of-the-new-c-4-0-dynamic-keyword/2243818#2243818

Upvotes: 3

amazedsaint
amazedsaint

Reputation: 7642

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();

Upvotes: 3

Kev
Kev

Reputation: 119856

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/

Upvotes: 3

Related Questions