3

A bit rusty in C#

I need to pass in a callback to a method:

void InvokeScript(string jsScript, Action<object> resultCallback);

In my class I have created a method to pass in 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>'

Thanks in advance

0

5 Answers 5

2

Your callback should be:

public void callback(object value)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to make the parameter your passing an object.

Comments

0

Either you create a method matching the signature of the Action<object> delegate, such as

public void someMethod(object parameter) { }

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

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

Comments

0

Try

Action<object> myCallBack = delegate
{
// do something here
};

    InvokeScript("some string", myCallBack);

The method delegate needs to take an object and not return any value. That's what Action<object> means. You can use the built-in Action delegate as I've shown, or you can create a new method and pass it as delegate:

public void MyMethod(object myParameter)
{
    // do something here.
}

InvokeScript("some string", MyMethod);

Comments

0

The signature of the callback should be the following:

void MethodName(object parameter);

You also may use lambda expression even without creating a separate method:

InvokeScript(
    "some string",
    p =>
    {
        // the callback's logic
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.