This project has moved. For the latest updates, please go here.

Returning a derived type with VBScript

May 23, 2014 at 5:55 AM
Edited May 23, 2014 at 6:47 AM
Currently I'm in the process of migrating my own VBScript ScriptingHost integration to ClearScript.

I discovered that the behaviour of ClearScript differs in (at least ;-) one way:

When returning a type from a function that is actually the derived type, ClearScript does not see the members of that derived type.

Example:
public class Base
{
    public int A() { return 1; }
}

public class Derived : Base
{
    public int B() { return 2; }
}
Using this class somewhat like:
...
public Base GetObject() { return new Derived(); }
...
And then inside my script:
...
dim x
set x = MyObject.GetObject();

x.B() ' Here it throws "Object does not support property or method".
...
I've also tried engine.AllowReflection = true; without any success.

While this behaviour is what I would expect from C# code, I would love to have ClearScript behave like my own VBScript ScriptingHost implementation and see all members, no matter what type the engine thinks it sees.

My question is:

Can I configure ClearScript to also make the members of derived classes be visible to the script, even when only dealing with a base type?
Coordinator
May 23, 2014 at 11:31 AM
Hello!

The quick answer: You can do this:
[ScriptMember(ScriptMemberFlags.ExposeRuntimeType)]
public Base GetObject() { return new Derived(); }
For more information, please see here.

Good luck!
May 23, 2014 at 12:13 PM
Awesome, thanks a lot.

Any chance to globally switch the default behaviour to always expose the runtime type?
Coordinator
May 24, 2014 at 2:55 PM
Unfortunately ClearScript doesn't have such an option at this time.
Coordinator
Jun 11, 2014 at 2:34 AM
We've posted an update that includes the new property ScriptEngine.DisableTypeRestriction for controlling this behavior at the engine level.
Jun 11, 2014 at 5:29 AM
Incredible! This is a huge improvement for me and helps me a lot.

Thank you very much!