Skip to content

Instantly share code, notes, and snippets.

@HectorMongoose
Last active August 29, 2015 14:01
Show Gist options
  • Save HectorMongoose/ddb7026cf348d27f5f97 to your computer and use it in GitHub Desktop.
Save HectorMongoose/ddb7026cf348d27f5f97 to your computer and use it in GitHub Desktop.
JObject.JObjectDynamicProxy.TryInvokeMember
// 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