Created
November 4, 2009 22:16
-
-
Save chrisforbes/226438 to your computer and use it in GitHub Desktop.
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
public static class ControllerInterface | |
{ | |
public static void DoHostCall(GeckoWebBrowser w, object controller) | |
{ | |
try | |
{ | |
var e = w.Document.GetElementsByTagName("callinfo").First(); | |
var methodName = e.GetAttribute("funcname"); | |
var method = controller.GetType().GetMethod(methodName); | |
if (method == null) | |
throw new InvalidOperationException( | |
"No such method `{0}` on managed controller".F(methodName)); | |
var argc = int.Parse(e.GetAttribute("argc")); | |
var args = Range(0, argc) | |
.Select(i => MarshalParamFromJs(e.GetAttribute("arg_" + i), | |
method.GetParameters()[i])); | |
var result = method.Invoke(controller, args.ToArray()); | |
if (result != null) | |
e.SetAttribute("retval", result.ToString()); | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show("{0}".F(e)); | |
} | |
} | |
static IEnumerable<int> Range(int low, int high) | |
{ | |
while (low < high) | |
yield return low++; | |
} | |
static object MarshalParamFromJs(string jsarg, ParameterInfo pi) | |
{ | |
if (pi.ParameterType == typeof(string)) | |
return jsarg; | |
if (pi.ParameterType == typeof(bool)) | |
return jsarg == "true"; | |
throw new Exception("You can't marshal this! Marshaling value `{0}` to `{1}`" | |
.F(jsarg, pi.ParameterType)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment