Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Duck Typing with the Dynamic Keyword

0.00/5 (No votes)
24 May 2013 1  
Don't do this at home.

Introduction

Disclaimer:

Kids, don't do try at home.  Yes, we're using keywords and classes you can easily find in the cupboards of your .NET language, but please do not mix these keywords as described here.  Potentially lethal (to your job and someone else's sanity) will result.  The programming you are about to witness has been done by a professional, stunted developer. 

Now, let's go blow up some of your dull ideas about programming!

One of the interesting things about languages like Ruby is Duck Typing, the "if it walks like a duck, if it talks like a duck, if you're yard is covered in duck s***, why then, it must be a duck!"  Of course, with strongly typed languages like C#, we just couldn't do some of the abusive amazing things you will see in weakly typed languages.  Instead, we've had to rely on object-oriented concepts like inheritance, polymorphism, and interfaces to achieve some small degree of coolness. 

But no more!  The world has become the playground of all things that go quack in the night with the advent of the "dynamic" keyword!

The Shackles Of Strong Typing...

Oh, the boring things we have to do for the sake of strong typing:

public interface IQuack
{
  void Talk();
}

public class Duck : IQuack
{
  public void Talk() {Console.WriteLine("Quack");}
}

public class Goose : IQuack
{
  public void Talk() { Console.WriteLine("Honk"); }
}

How They Bind Us, Constrain Us...

static void Speak(IQuack bird)
{
  bird.Talk();
}

IQuack bird1 = new Duck();
IQuack bird2 = new Goose();
Speak(bird1);
Speak(bird2);

Quack
Honk

But Free Us From The Shackles of Your Interfaces...

public class DynoDuck
{
  public void Talk() { Console.WriteLine("Dyno-Quack!"); }
}

public class DynoGoose
{
  public void Talk() { Console.WriteLine("Dyno-Honk!"); }
}

And We Become Free Men (and Women, and Birds)! 

static void Speak(dynamic d)
{
  d.Talk();
}

dynamic dynoBird1 = new DynoDuck();
dynamic dynoBird2 = new DynoGoose();
Speak(dynoBird1);
Speak(dynoBird2);

Dyno-Quack!
Dyno-Honk!

And Free of All Constraints Of Race, Creed, or...Class!

static void Speak(dynamic d, string outWithIt)
{
  d.Talk(outWithIt);
}

static dynamic InMyImage(string method, Action<string> action)
{
  dynamic dyn = new ExpandoObject();
  ((IDictionary<String, Object>)dyn)[method] = action;
  return dyn;
} 

To Soar with the Gods!

var actor = (Action<string>) ((mySpeech) => Console.WriteLine(mySpeech));

dynamic dyn1 = InMyImage("Talk", actor);
dynamic dyn2 = InMyImage("Talk", actor);

Speak(dyn1, "Rise, my fellow ducks!");
Speak(dyn2, "To Canada or bust!"); 

Rise, my fellow ducks!
To Canada or bust! 

And Like Icarus, To Crash Back To Earth

As we realize freedom comes with a price: complete coding chaos! 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here