4

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:

4 Answers 4

3

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/

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

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

2 Comments

Clever - I'm going to try that myself.
Update: I am not doing this anymore. I found it easier to generate an empty class and let test fail in other ways. Proper TDD ;-)
2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.