This project has moved and is read-only. For the latest updates, please go here.

invoking js callback with params argument

Oct 31, 2015 at 12:26 PM
Edited Oct 31, 2015 at 1:09 PM

i want to invoke an js callback from c# with unknown arguments count

public void Fire(string name, params object[]args)
            if (_listeners.ContainsKey(name))
                foreach (var l in _listeners[name])
but all arguments are in js is undefined
Oct 31, 2015 at 6:06 PM
Edited Oct 31, 2015 at 6:11 PM

One way to do this is to create a general-purpose dynamic invocation helper:
// using System.Dynamic;
// using System.Linq;
// using Microsoft.CSharp.RuntimeBinder;
public static class ObjectHelpers {
    private static readonly CSharpArgumentInfo argInfo = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
    public static object DynamicInvoke(object target, params 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;
        throw new InvalidOperationException("Invocation failed");
Then, instead of l(args), use ObjectHelpers.DynamicInvoke(l, args).

Good luck!
Nov 1, 2015 at 2:54 PM
thanks a lot it works