Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: invoking js callback with params argument

0
0
Hello!

One way to do this is to create a general-purpose dynamic invocation helper:
// using System.Dynamic;// using System.Linq;// using Microsoft.CSharp.RuntimeBinder;publicstaticclass ObjectHelpers {
    privatestaticreadonly CSharpArgumentInfo argInfo = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
    publicstaticobject DynamicInvoke(object target, object[] args) {
        var del = target as Delegate;
        if (del != null)
            return del.DynamicInvoke(args);
        var dynamicObject = target as DynamicObject;
        if (dynamicObject != null) {
            object result;
            var binder = Binder.Invoke(CSharpBinderFlags.None, null, Enumerable.Repeat(argInfo, args.Length));
            if (dynamicObject.TryInvoke((InvokeBinder)binder, args, out result))
                return result;
        }
        thrownew InvalidOperationException("Invocation failed");
    }
}
Then, instead of l(args), use ObjectHelpers.DynamicInvoke(l, args).

Good luck!

Viewing all articles
Browse latest Browse all 2297