David Neale
David Neale

Reputation: 17048

Most common applications of the C# 4.0 dynamic type

Now that people have been using C# 4.0 for a while I thought I'd see how people were most often using the type 'dynamic' and why has this helped them solve their problem better than they may have done previously?

Upvotes: 5

Views: 172

Answers (2)

Jason Miesionczek
Jason Miesionczek

Reputation: 14448

It's also used when embedding dynamic languages such as IronPython/IronRuby to allow loading types defined in external source files, and accessing them more directly in C#

Upvotes: 2

Rody
Rody

Reputation: 2655

For example when using reflection.

Example, something like this:

object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });
int sum = Convert.ToInt32(res);

would than become:

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

That's a big improvement I think.

But there are more subjects where this can come in handy. For instance when working with COM interop objects this could come in handy, look at: http://www.devx.com/dotnet/Article/42590

Upvotes: 3

Related Questions