Comments (10)
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.
@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.
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.
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.
from nlua.
I tried it, it just throws the exception I said in the issue
from nlua.
from nlua.
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.
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.
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)
- Registering any function causes all `MethodBase` properties and recursive properties to be registered as well
- List Indexer return null? (Question) HOT 1
- c#使用报错 试图加载格式不正确的程序 HOT 1
- Alwasy got Invalid arguments to method HOT 2
- AccessViolationException HOT 1
- The function name does not support Chinese,
- Add an attribute to allow developers to choose member names when registered in Lua
- how can i push LuaScriptException ? HOT 1
- Unable to pass Lua functions as parameters to the Delegate class
- How can I get all variable objects from the lua file in c# ?
- Concat operator does only tries to invoke metamethod from lhs
- Question about execution scopes HOT 1
- compatibility with KeraLua
- AOT support? HOT 1
- Lua import function not working property on Linux/MacOS HOT 4
- Heavy Duty Persistence
- GetTable Documentation
- Add `LuaBase.Interpreter` property
- How do I specify optional arguments for a function?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nlua.