This project has moved. For the latest updates, please go here.

Calling a generic C# method from javascript

May 28, 2014 at 6:28 AM
Edited May 28, 2014 at 6:29 AM

Is it possible to call a generic method ? as am not able to figure out a way to do the same.
Here is my case:
class Class1
public T WorkOnType<T>(){ //do something and return T}
When initializing V8ScriptEngine, I add a host object for Class1 say class1Obj,

how would i call class1Obj.WorkOnType() from javascript ?

I know that i can add an overload of WorkOnType(Type T), but in case I don't want to change anything at C# end, is there
any other way ?
May 28, 2014 at 6:41 AM
can i use gernic in jscript or vbscript engine?
May 28, 2014 at 8:21 AM
Hi furesoft,

I asked this because documentation says generic methods are supported.
May 28, 2014 at 8:33 AM
Edited May 28, 2014 at 8:33 AM
i know i would know too how can i use
May 28, 2014 at 8:57 AM
One way could be to add a host type and pass it to the generic method ?
May 28, 2014 at 12:49 PM
Hello maicalal,

One way could be to add a host type and pass it to the generic method ?

That's correct. Many generic methods don't require explicit type arguments because their type arguments can be inferred from their other arguments. In this case there are no other arguments, so type inference is impossible and an explicit type argument is required.

Here's a simple example:
public class Factory {
    public T Create<T>() where T : new() {
        return new T();
public class SomeClass {
    // ...
and then:
engine.AddHostObject("factory", new Factory());
engine.AddHostType("SomeClass", typeof(SomeClass));
engine.Execute("obj = factory.Create(SomeClass)");
In general, to invoke a method that requires explicit type arguments, place the required number of host types at the beginning of the argument list.