Created
September 22, 2015 19:24
-
-
Save fuzzykiller/f7c2f6ee103b13819987 to your computer and use it in GitHub Desktop.
Dynamic objects in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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