-
-
Notifications
You must be signed in to change notification settings - Fork 307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you pass functions as delegates? #491
Comments
If is an |
@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? |
In the meantime, I wrote myself a quick workaround, but I wonder if there is any intended way to do what I asked |
My workaround for those interested:
|
iirc Lua functions can be used to places you expecting a delegate
…On Tue, Jun 6, 2023 at 11:28 AM vlOd ***@***.***> wrote:
My workaround for those interested:
Script["ControlInvoke"] = new Action<Control, Action>((Control control, Action action) => control.Invoke(action));
—
Reply to this email directly, view it on GitHub
<#491 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHCJFBXIVQPUJ7SKNOWOX3XJ5ZFHANCNFSM6AAAAAAY4WJRBM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I tried it, it just throws the exception I said in the issue |
I am not sure what you trying to do. Write a small test case
…On Fri, Jun 9, 2023 at 3:09 PM vlOd ***@***.***> wrote:
I tried it, it just throws the exception I said in the issue
—
Reply to this email directly, view it on GitHub
<#491 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHCJFCNL722B37XZ3BPITTXKNREVANCNFSM6AAAAAAY4WJRBM>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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 ( |
Here is a test case, the method 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();
}
} |
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)
|
How do you pass functions as delegates to C# methods/classes that expect a delegate?
For example, there is the winforms method for controlls called Invoke that expects a Delegate
In C#, you'd normally pass your function as an instance of Action by doing Action(My Function)
But how can you do this in Lua? I tried just Invoke(My Function) but that gives
Invalid arguments to method: Control.Invoke
and I also tried creating an Action by doing Invoke(Action(My Function)) but that givesSystem.Action does not contain constructor(.ctor) argument match
Also
My Function
is just a placeholderThe text was updated successfully, but these errors were encountered: