I use MS ClearScript library for some dynamic parts in my system. I have some list of objects that already created in .NET(C#) code, this objects use as description for functions use. Like this (Javascript):
var form = MetaData.GetClass('MyFormClassName', 'FormId'); form.Show();
In this example a get a class MyFormClassName with ID FormId with the static method MetaData.GetClass. The method returns an instance of MyFormClassName with ID FormId created in.NET(C#) code. There is a method Show() that show windows form with parameters in an instance.
But when I call a Show() method I get an error:
form.Show is not a function
The engine contains an object Properties, but there are no methods. Engine don't know object Type. When I use AddHostObject with name form it works fine, but I do not know what variable name will be used in future.
Does any body know how I can use .NET (C#) objects methods in V8ScriptEngine? Is it possible don't register host object every time for object methods use?
The most likely problem is that the
MetaData.GetClassreturn value type is a base class or interface that doesn't have aShowmethod.If that's correct, then
Showis provided by the actual type of the returned object, and downcasting is required to call it.There are several ways to deal with this:
HostFunctions.castorHostFunctions.asTypeto downcast in JavaScript.ScriptMember(ScriptMemberFlags.ExposeRuntimeType)to yourMetaData.GetClassimplementation.ScriptEngine.DisableTypeRestrictiontotrue(not recommended).