Handling JavaScript functions and closures passed into C# function

  • Thread starter Thread starter Nathan Baker
  • Start date Start date
N

Nathan Baker

I have a JavaScript environment using IActiveScript and friends (via
COM interop). At one point, the JavaScript passes me a function as a
parameter to an object that has been inserted into the JavaScript
using AddNamedItem.

When I look at this object and ask it for its type, it says it is a
System.__ComObject. What I want to do is invoke this function in the
environment that I have using the engine I have instantiated. Is there
a way to do this?

Thanks,
Nathan
 
Nathan,

You should be able to invoke the default member of the object in order
to invoke the method through reflection. IIRC, when a function is passed to
you, it is passed as an object with an IDispatch interface implementation
with the default method (with dispid = 0) being how you would call the
function.
 
Thanks a bunch, Nicholas. It worked exactly like you said.

For posterity, here's the code. "command" is the command passed in
from JavaScript and stored as an object, and jsData is some other
stuff passed in from JavaScript:
==
Type t = typeof(stdole.IDispatch);
t.InvokeMember("",
System.Reflection.BindingFlags.InvokeMethod,
null,
command,
new object[] { jsData });
==
Passing the null string for the first parameter invokes the default
method (as mentioned in the documentation).

Thanks again for pointing me in the right direction.

Nathan

Nathan,

You should be able to invoke the default member of the object in order
to invoke the method through reflection. IIRC, when a function is passed to
you, it is passed as an object with an IDispatch interface implementation
with the default method (with dispid = 0) being how you would call the
function.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have a JavaScript environment using IActiveScript and friends (via
COM interop). At one point, the JavaScript passes me a function as a
parameter to an object that has been inserted into the JavaScript
using AddNamedItem.
When I look at this object and ask it for its type, it says it is a
System.__ComObject. What I want to do is invoke this function in the
environment that I have using the engine I have instantiated. Is there
a way to do this?
Thanks,
Nathan
 
Nathan,

You probably don't need the type of IDispatch, rather, you can just call
command.GetType().


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nathan Baker said:
Thanks a bunch, Nicholas. It worked exactly like you said.

For posterity, here's the code. "command" is the command passed in
from JavaScript and stored as an object, and jsData is some other
stuff passed in from JavaScript:
==
Type t = typeof(stdole.IDispatch);
t.InvokeMember("",
System.Reflection.BindingFlags.InvokeMethod,
null,
command,
new object[] { jsData });
==
Passing the null string for the first parameter invokes the default
method (as mentioned in the documentation).

Thanks again for pointing me in the right direction.

Nathan

Nathan,

You should be able to invoke the default member of the object in
order
to invoke the method through reflection. IIRC, when a function is passed
to
you, it is passed as an object with an IDispatch interface implementation
with the default method (with dispid = 0) being how you would call the
function.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have a JavaScript environment using IActiveScript and friends (via
COM interop). At one point, the JavaScript passes me a function as a
parameter to an object that has been inserted into the JavaScript
using AddNamedItem.
When I look at this object and ask it for its type, it says it is a
System.__ComObject. What I want to do is invoke this function in the
environment that I have using the engine I have instantiated. Is there
a way to do this?
Thanks,
Nathan
 
Nicholas,

I figured that was probably the case, but I wasn't sure, so I went
ahead and took the safe route :)

Thanks again,
Nathan

Nathan,

You probably don't need the type of IDispatch, rather, you can just call
command.GetType().

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks a bunch, Nicholas. It worked exactly like you said.
For posterity, here's the code. "command" is the command passed in
from JavaScript and stored as an object, and jsData is some other
stuff passed in from JavaScript:
==
Type t = typeof(stdole.IDispatch);
t.InvokeMember("",
System.Reflection.BindingFlags.InvokeMethod,
null,
command,
new object[] { jsData });
==
Passing the null string for the first parameter invokes the default
method (as mentioned in the documentation).
Thanks again for pointing me in the right direction.
Nathan,
You should be able to invoke the default member of the object in
order
to invoke the method through reflection. IIRC, when a function is passed
to
you, it is passed as an object with an IDispatch interface implementation
with the default method (with dispid = 0) being how you would call the
function.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a JavaScript environment using IActiveScript and friends (via
COM interop). At one point, the JavaScript passes me a function as a
parameter to an object that has been inserted into the JavaScript
using AddNamedItem.
When I look at this object and ask it for its type, it says it is a
System.__ComObject. What I want to do is invoke this function in the
environment that I have using the engine I have instantiated. Is there
a way to do this?
Thanks,
Nathan
 
Back
Top