Open herki18 opened 1 day ago
Hi @herki18,
The behavior you're seeing is by design. It's an effect of a ClearScript feature called type restriction, which enhances the ability of script languages to consume strongly typed .NET APIs.
// This fails because methods specific to HtmlDivElement are not bound
div.setDivSpecificMethod(); // Error: setDivSpecificMethod is not a function
That's right. The variable div
is effectively of type IElement
. If this were C# code, you couldn't call SetDivSpecificMethod
without casting, and the same applies to JavaScript.
Type restriction is important for .NET member binding. Suppose you wanted to pass div
to a .NET method overloaded as follows:
void DoSomething(IElement element);
void DoSomething(HtmlDivElement element);
Without type restriction, the first overload would be inaccessible, and that's just the tip of the iceberg. .NET APIs often rely heavily on precise type tracking for correct member binding, ambiguity resolution, type inferencing, generic type and method instantiation, etc. ClearScript aims not only to make such APIs usable from script code but also to make them behave as .NET developers would expect.
That said, ClearScript does give hosts some control over type restriction:
object
are exempt from type restriction.HostFunctions.cast
is a script-callable type casting function.ScriptMemberAttribute.Flags
can disable type restriction for an individual member (via ScriptMemberFlags.ExposeRuntimeType
).ScriptEngine.DisableTypeRestriction
provides control at the script engine level.Good luck!
I may have misunderstood how binding works in ClearScript, but I'm encountering an issue where methods from derived classes are not accessible. We have a method createElement that returns an object of type IElement. However, the actual object being returned is an instance of a derived class, HtmlDivElement, which inherits from IElement. The problem is that ClearScript only binds methods and properties from IElement and its ancestors. This results in methods specific to HtmlDivElement being inaccessible from JavaScript.
Example:
JavaScript Example:
Question:
How can I ensure that ClearScript binds methods from the derived class (
HtmlDivElement
) so that they are accessible in JavaScript? Is there a way to expose these methods while keeping the inheritance structure intact?