ClearsSript V8ScriptEngine TypeError: form.Show is not a function

720 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

The most likely problem is that the MetaData.GetClass return value type is a base class or interface that doesn't have a Show method.

If that's correct, then Show is provided by the actual type of the returned object, and downcasting is required to call it.

There are several ways to deal with this:

  1. Use HostFunctions.cast or HostFunctions.asType to downcast in JavaScript.
  2. Add the attribute ScriptMember(ScriptMemberFlags.ExposeRuntimeType) to your MetaData.GetClass implementation.
  3. Set ScriptEngine.DisableTypeRestriction to true (not recommended).