Giter Site home page Giter Site logo

Comments (10)

viniciusjarina avatar viniciusjarina commented on May 27, 2024

If is an event you can use Add to subscribe to it
like here https://github.com/NLua/NLua/blob/main/extras/examples/form_alt.lua#L27

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

@viniciusjarina That is not the solution, I do know that events can be subscribed to with Add and unsubscribed from by passing the EventHandler returned from add to Remove, I am asking how to create delegate Action and pass that to the Invoke method on controls?

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

In the meantime, I wrote myself a quick workaround, but I wonder if there is any intended way to do what I asked

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

My workaround for those interested:

// You should probably put this in a different table rather than global, like how I did in the code where I use this, but this works too
Script["ControlInvoke"] = new Action<Control, Action>((Control control, Action action) => control.Invoke(action));

from nlua.

viniciusjarina avatar viniciusjarina commented on May 27, 2024

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

I tried it, it just throws the exception I said in the issue

from nlua.

viniciusjarina avatar viniciusjarina commented on May 27, 2024

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

I am trying to pass a method to a Delegate parameter, for example System.Windows.Forms.Control.Invoke() takes a delegate, and I tried passing the method directly (Invoke(MyFunc)) but it threw Invalid arguments to method: Control.Invoke and I also tried wrapping it in an Action (Invoke(Action(MyFunc))) as you'd normally do in C#, but that threw System.Action does not contain constructor(.ctor) argument match

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

Here is a test case, the method MyMethodThatTakesADelegate has the arguments as one of the overloads of System.Windows.Forms.Control.Invoke

class Program
{
    static void Main(string[] args)
    {
        MyMethodThatTakesADelegate(new Action(() => 
        {
            Console.WriteLine("Hello from C#!");
        }));

        Lua lua = new Lua();
        lua.LoadCLRPackage();
        lua["MyMethodThatTakesADelegate"] = (Action<Action>)MyMethodThatTakesADelegate;

        try
        {
            /*
             * MyMethodThatTakesADelegate(function() 
             *      print("Hello, world")
             * end)
            */
            lua.DoString("MyMethodThatTakesADelegate(function() print(\"Hello, world\")end)");
        }
        catch (Exception ex) { Console.Error.WriteLine($"First test case failed: {ex}"); }

        try 
        {
            /*
             * import("System")
             * MyMethodThatTakesADelegate(Action(function() 
             *      print("Hello, world")
             * end))
            */
            lua.DoString("import(\"System\") MyMethodThatTakesADelegate(Action(function() print(\"Hello, world\") end))");
        }
        catch (Exception ex) { Console.Error.WriteLine($"Second test case failed: {ex}"); }
        Console.ReadLine();
    }

    public static void MyMethodThatTakesADelegate(Delegate myDelegate) 
    {
        myDelegate.DynamicInvoke();
    }
}

from nlua.

vlOd2 avatar vlOd2 commented on May 27, 2024

Output of test case using the latest version of NLua on .NET 4.7.2 (also on .NET 4.6 where I first discovered this bug/lack of support)

Hello from C#!
First test case failed: NLua.Exceptions.LuaScriptException: [string "chunk"]:1: Cannot invoke delegate (invalid arguments for  MyMethodThatTakesADelegate)
Second test case failed: NLua.Exceptions.LuaScriptException: [string "chunk"]:1: System.Action does not contain constructor(.ctor) argument match

from nlua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.