How to call methods with Type arguments? #506
-
If I try call a method which takes a
I guess this happens since ClearScript thinks I'm trying to call a generic method. What is the way to call these types of methods? Repro: namespace ClearScriptPlaygroud;
public class ScriptObject
{
public void DoSomethingWithType(Type aType) { }
}
internal class Program
{
static void Main(string[] args)
{
using var engine = new V8ScriptEngine();
engine.AddHostObject("host", new HostFunctions());
var typeCollection = new HostTypeCollection("mscorlib");
engine.Script["System"] = typeCollection.GetNamespaceNode("System");
var obj = new ScriptObject();
engine.AddHostObject("obj", obj);
engine.Execute(@"obj.DoSomethingWithType(System.String.Type);");
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi @handerss-tibco,
Not quite. Consider this line in your code: engine.Execute(@"obj.DoSomethingWithType(System.String.Type);"); .NET's
In the JavaScript environment, Suppose you wanted to call obj.DoSomethingWithType(string); Instead, you'd use the obj.DoSomethingWithType(typeof(string)); In JavaScript, you can use ClearScript's engine.AllowReflection = true;
engine.Execute(@"obj.DoSomethingWithType(host.typeOf(System.String));"); Good luck! |
Beta Was this translation helpful? Give feedback.
-
Ah. In that case, you could expose the required Cheers! |
Beta Was this translation helpful? Give feedback.
Hi @handerss-tibco,
Not quite. Consider this line in your code:
.NET's
String
class has no static property named "Type", so the JavaScript expressionSystem.String.Type
evaluates toundefined
, which isn't a valid argument forDoSomethingWithType
.In the JavaScript environment,
System.String
is a host type; it isn't aType
object. A host type is a special object that enables C#-like syntax and capabilities.Suppose you wanted to call
DoSomethingWithType
in C#. The following wouldn't work: