Last active
August 29, 2015 14:01
-
-
Save HectorMongoose/ddb7026cf348d27f5f97 to your computer and use it in GitHub Desktop.
JObject.JObjectDynamicProxy.TryInvokeMember
This file contains 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
// Not sure if you'll like this at all, but adding this method to JObject.JObjectDynamicProxy | |
// allows us to use the invoked method as the default parameter, if the value does not exist. | |
// For example: | |
dynamic parsedObject = new JObject(); | |
d.test = "Non-default option"; | |
Console.WriteLine("Test is: " + d.test("Default Option")); | |
// Outputs: | |
// Test is: Non-default option | |
dynamic parsedObject = new JObject(); | |
Console.WriteLine("Test is: " + d.test("Default Option")); | |
Console.WriteLine("Attempt 2: " + d.test); | |
// Outputs: | |
// Test is: Default Option | |
// Attempt 2: Default Option | |
public override bool TryInvokeMember(JObject instance, InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
if (instance[binder.Name] == null) | |
{ | |
result = instance[binder.Name] = JToken.FromObject(args[0]); | |
return true; | |
} | |
result = instance[binder.Name]; | |
return true; | |
} | |
// I'm too lazy to version control atm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment