views:

335

answers:

5

So the next version of C# will support dynamic binding through the dynamic keyword.

Microsoft has especially talked about how this will make the syntax for COM interop nicer and it is of course necessary for interop with the dynamically typed .NET languages.

But what else do you think we will be able to do with this new feature?

+2  A: 

It will also be easier to interop with objects that come from DLR languages (IronPython, IronRuby, etc)

Lou Franco
A: 

One obvious application would be a XML parser with natural syntax that would not need codegeneration, i.e. something like:

dynamic xml = DynamicXmlParser.Parse("<Customer><Name>Thomas</Name></Customer/>");
Assert.AreEqual("Thomas", xml.Customer.Name);
Rasmus Faber
A: 

My initial thought was something that would allow me to call an overloaded method with a type without creating many functions, such as this:

public dynamic GetResult(dynamic a, dynamic b)
{
   return MyLibrary.Process(a, b);
}

The point is that MyLibrary might fx. specify overloads for Process for the types int, double and float. With dynamics I dont have to create three overloaded methods.

It could be used like this also (I think, correct me if I'm wrong):

public dynamic Add(dynamic a, dynamic b)
{
   return a + b;
}
Morten Christiansen
This is really not the way to use it. C# 4.0 will support optional parameters and named parameters by default, removing the need for multiple overloads in many cases. This is the case where you have many overloads because of the signature. Your problem should be solved using Generics; I think.
TimothyP
+1  A: 

Perhaps I'm going against the grain with some people, but I prefer to have as much work done to cause errors to Fail Early. I love with my ORM generator that if someone mucks with the database that my code that was written based on the entity members will fail to compile rather than being strings that hide the error until they are used.

filter.PredicateExpression.Add(QuoteFields.FieldName == someValue)

vs

filter.PredicateExpression.AddEquals("FieldName", someValue)

The former will let me know if FieldName was lost or renamed (and in the case of a rename, I can use my refactoring tools to fix the entire project at once).

Moving to more strings that do magical things? Not my preference. On the other hand, COM interop and dynamic language interop are wonderful.

Godeke
Yes. The goal for the C# language in implementing the dynamic keyword is *not* to turn previously static situations into dynamic ones, but to take situations that are already dynamic and turn them into something much easier for programmers to work with.
Curt Hagenlocher
+1  A: 

I have found it also simplifies reflection.

//C# 3.0 version
compositeType = plugin.CompositeType;
MethodInfo method = compositeType.GetMethod("Compose");
var commands = (List<Command>)method.Invoke(compositeInstance,null);

//C# 4.0 version
dynamic compositeInstance = plugin.CompositeInstance
var commands = (List<Command>)compositeInstance.Compose();

It this example the difference is minimal, but still the code is cleaner and there are situations where the difference is huge.

Also, it's important to note that unlike "var", "dynamic" is not a keyword, but a type.

TimothyP