tags:

views:

1033

answers:

5

C# 4 will contain a new dynamic keyword that will bring dynamic language features into C#.

How do you plan to use it in your own code, what pattern would you propose ? In which part of your current project will it make your code cleaner or simpler, or enable things you could simply not do (outside of the obvious interop with dynamic languages like IronRuby or IronPython)?

PS : Please if you don't like this C# 4 addition, avoid to bloat comments negatively.

Edit : refocussing the question.

The classic usages of dynamic are well known by most of stackoverflow C# users. What I want to know is if you think of specific new C# patterns where dynamic can be usefully leveraged without loosing too much of C# spirit.

+1  A: 

I will use it to simplify my code which deals with COM/Interop where before I had to specify the member to invoke, its parameters etc. (basically where the compiler didn't know about the existence of a function and I needed to describe it at compile time). With dynamic this gets less cumbersome and the code gets leaner.

Alex
Excelent blog entry and code that will simplify Interop a lot can be found here: http://tirania.org/blog/archive/2009/Aug-11.html
Yakeen
+6  A: 

Wherever old-fashioned reflection is used now and code readability has been impaired. And, as you say, some Interop scenarios (I occasionally work with COM).

That's pretty much it. If dynamic usage can be avoided, it should be avoided. Compile time checking, performance, etc.

A few weeks ago, I remembered this article. When I first read it, I was frankly apalled. But what I hadn't realised is that I didn't know how to even use an operator on some unknown type. I started wondering what the generated code would be for something like this:

dynamic c = 10;
int b = c * c;

Using regular reflection, you can't use defined operators. It generated quite a bit of code, using some stuff from a Microsoft namespace. Let's just say the above code is a lot easier to read :) It's nice that it works, but it was also very slow: about 10,000 times slower than a regular multiplication (doh), and about 100 times slower than an ICalculator interface with a Multiply method.

Edit - generated code, for those interested:

if (<Test>o__SiteContainer0.<>p__Sitea == null)
  <Test>o__SiteContainer0.<>p__Sitea =
    CallSite<Func<CallSite, object, object, object>>.Create(
      new CSharpBinaryOperationBinder(ExpressionType.Multiply,
        false, false, new CSharpArgumentInfo[] {
          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null),
          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) }));
b = <Test>o__SiteContainer0.<>p__Site9.Target(
      <Test>o__SiteContainer0.<>p__Site9,
      <Test>o__SiteContainer0.<>p__Sitea.Target(
        <Test>o__SiteContainer0.<>p__Sitea, c, c));
Thorarin
I you think of cases where using reflection can be replaced by dynamic code, you can give a sample ?
Think Before Coding
@Think: It could be used in cases where functions are being reflectively invoked, not where reflection is being used to bypass accessibility (unless, I guess, you cast it to an object first? not sure...) or to do inspection.
Adam Robinson
+4  A: 

The dynamic keyword is all about simplifying the code required for two scenarios:

  • C# to COM interop
  • C# to dynamic language (JavaScript, etc.) interop

While it could be used outside of those scenarios, it probably shouldn't be.

Scott Dorman
have you got more information regarding your second point please scott? an C# managed game engine - exposing to an ajax graphics engine is something I am working on and would love to see how this would help!
divinci
+4  A: 

Miguel de Icaza presented a very cool use case on his blog, here (source included):

dynamic d = new PInvoke ("libc");
d.printf ("I have been clicked %d times", times);

If it is possible to do this in a safe and reliable way, that would be awesome for native code interop.

Trillian
+1  A: 

This will also allow us to avoid having to use the visitor pattern in certain cases as multi-dispatch will now be possible

public class MySpecialFunctions
{
  public void Execute(int x) {...}
  public void Execute(string x) {...}
  public void Execute(long x) {...}
}

dynamic x = getx();
var myFunc = new MySpecialFunctions();
myFunc.Execute(x);

...will call the best method match at runtime, instead of being worked out at compile time

saret
Please help me understand the advantage here. Why would you not want this to be resolved at compile time?
Cipher
Sometimes you don't know what type you are working with - for example you're working with a baseclass but need special behaviour for subtypes.When transversing a tree structure, each node might inherit off a base class node, but when processing the node you need specific behaviour for subtypes (ie. composite node). You can utilize a class like MySpecialFunctions and call Execute without huge switch/if blocks trying to work out which method to call by checking the type of the node being proccessed. The client code doesn't have to be aware of all subtypes to utilize MySpecialFunctions correctly
saret
Also new/alternate subtypes of MySpecialFunctions would not require changes to client code proccessing nodes.
saret