Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Created January 19, 2011 03:59
Show Gist options
  • Save chrisnicola/785667 to your computer and use it in GitHub Desktop.
Save chrisnicola/785667 to your computer and use it in GitHub Desktop.
Some examples of how the dynamic keyword works
[Fact]
public void cant_pass_base_class_to_function_dynamically()
{
dynamic handler = new MessageHandler();
IMessage message = new Message();
handler.Handle(message);
}
[Fact]
public void can_pass_base_class_to_function_via_reflection()
{
var handler = new MessageHandler();
IMessage message = new Message();
handler.GetType().GetMethod("DoSomething").Invoke(handler, new[] { message });
}
[Fact]
public void can_pass_dynamic_type_to_funcation_dynamically()
{
dynamic handler = new MessageHandler();
dynamic message = new Message();
handler.Handle(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment