This project is read-only.

Error Calling C# Function From Javascript

Apr 18, 2012 at 9:00 PM

Hi all,

I'm attempting to use C# with Javascript .NET to interact with a web service which passes back a string with two substrings: an ID and a Javascript function to execute. For example, the string could look like:

 

"NS.dispatch('12345',function(){\nthis.myFunc(false,\"1\");})"

 

I'd like to arrange it so that this Javascript dispatch will call a C# dispatch and handle things appropriately. What's happening now, though, is that the C# dispatch is being called with the appropriate first parameter (the ID) but the second parameter (the function) is coming across as:

 

"System.Collections.Generic.Dictionary`2[System.String,System.Object]"

 

I want it to come across as:

'12345','function(){\nthis.myFunc(false,\"1\");}'

I'm setting things up with:

_js.Run("function dispatch(v, w) { NS.dispatch(v, w); }");

And my C# dispatch signature is:

public void dispatch(string poid, string script){}
Any ideas how I can force the string to come through to the C# dispatch as an actual string and not as the Generic.Dictionary? It seems like it's trying to figure out what type to pass it as rather than just passing it as a string. Thanks for the help.

Apr 19, 2012 at 12:22 AM

The second parameter to NS.dispatch is an anonymous function, not a string.  Javascript.NET is treating it as a JavaScript object, which is why you see the dictionary.  If the original input was something like the below then you would get what you want.

"NS.dispatch('12345','function(){\nthis.myFunc(false,\"1\");}')"