Skip to content

Instantly share code, notes, and snippets.

@fuzzykiller
Created September 22, 2015 19:24
Show Gist options
  • Save fuzzykiller/f7c2f6ee103b13819987 to your computer and use it in GitHub Desktop.
Save fuzzykiller/f7c2f6ee103b13819987 to your computer and use it in GitHub Desktop.
Dynamic objects in C#
private static void DoStuff()
{
var stuff = GetStuff();
//// This won't compile:
//string myText = stuff.Banana;
dynamic dynamicStuff = stuff;
//// This compiles, because it uses Reflection to retrieve the property.
//// It will fail at runtime though, because 'Banana' obviously isn't a number.
int myNumber = dynamicStuff.Banana;
}
private static object GetStuff()
{
return new { Banana = "Yes" };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment