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


Your callback should be:



public void callback(object value)

      

+2


source


You need to make the parameter you are passing object

.



0


source


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


Either you create a method that matches the delegate signature Action<object>

, like

public void someMethod(object parameter) { }

      

and then pass it,
or you can use a lambda:

InvokeScript("stuff", 
    param => { 
        Blah(param); 
        MoreBlah();
    });

      

0


source


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







All Articles