Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Calling a generic C# method from javascript

$
0
0
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:
publicclass Factory {
    public T Create<T>() where T : new() {
        returnnew T();
    }
}
publicclass 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.

Cheers!

Viewing all articles
Browse latest Browse all 2297

Latest Images

Trending Articles





Latest Images