C # callback parameter
A bit rusty in C #
I need to pass a callback to a method:
void InvokeScript(string jsScript, Action<object> resultCallback);
In my class, I created a method to navigate to the method:
public void callback(System.Action<object> resultCallback)
{
}
Error message 1:
Resco.UI.IJavascriptBridge.InvokeScript(string, System.Action<object>)' has some invalid arguments
Error message 2:
cannot convert from 'method group' to 'System.Action<object>'
Thank you in advance
+3
source to share
5 answers
Try
Action<object> myCallBack = delegate
{
// do something here
};
InvokeScript("some string", myCallBack);
The method delegator needs to accept object
and not return any value. What it means Action<object>
. You can use the built-in delegate Action
as I showed, or you can create a new method and pass it as a delegate:
public void MyMethod(object myParameter)
{
// do something here.
}
InvokeScript("some string", MyMethod);
0
source to share
The callback signature should be as follows:
void MethodName(object parameter);
You can also use lambda expression even without creating a separate method:
InvokeScript(
"some string",
p =>
{
// the callback logic
});
0
source to share