Closed EtienneLaneville closed 2 months ago
Hi @EtienneLaneville,
This code throws the following exception:
Hmm, we can't reproduce that. Our C# app displays a "Person 0" message box five times.
An exception like the one above often indicates a type or member visibility issue. Are you sure that your Person
class and its Name
property were both public
when you ran your test?
Since I am creating a new instance of the Person class in every iteration and using
AddHostObject
with the new instance, I would expect it to display "Person 1", "Person 2", "Person 3", "Person 4", "Person 5" and finally "Person 5".
Ah, OK. What you're seeing is actually expected. You've discovered one of ClearScript's dark corners 😱
When you call a Windows Script engine's AddHostObject
or AddHostType
method, you're creating a so-called host item. One of the ways in which a host item differs from other external objects is that the engine expects it not to change.
Because of that assumption, the engine retrieves a host item's value only once – the first time a script accesses it – and caches it thereafter. That's why the scripts in your test app always see the same Person
object.
Host items are a very old Windows Script feature that's meant to be used for host APIs rather than data. ClearScript uses them because it's the only way to access certain functionality – e.g., GlobalMembers
.
My ultimate goal is to iterate through a collection of objects, adding them one by one to the script engine, and executing user-defined VBScript code. The user code handles each object one at a time and expects the object to be named the same.
That's no problem; instead of calling AddHostObject
, you can do this:
host.Global.SetProperty("Person", person);
Please give that a try. It should behave as you expect.
Good luck!
Indeed, regarding the Person.Name
exception, the Person
class was defined within Program.cs
. Moving it out resolved the issue.
Also, host.Global.SetProperty("Person", person)
works as expected. Thanks for pointing me in the correct direction.
I adjusted the question title to reflect the second issue, I think it could be more helpful to others.
I am running into a strange issue with VBScriptEngine (using Microsoft.ClearScript.Windows 7.4.5 package from NuGet). I have the following
Person
class that has a singleName
property:I also have a
HostWindow
class that implementsIHostWindow
so I can useMsgBox
in my script:With these, I am trying to test what happens when I use
AddHostObject
multiple times using the same object name:This code throws the following exception:
I have the same code written in a separate Visual Basic .Net project and it works properly there:
This code does not throw the same exception. Instead, it displays "Person 1" 6 times. Since I am creating a new instance of the Person class in every iteration and using
AddHostObject
with the new instance, I would expect it to display "Person 1", "Person 2", "Person 3", "Person 4", "Person 5" and finally "Person 5".If I remove the
MsgBox
from inside theFor ... Next
loop:it displays "Person 5". This tells me that
AddHostObject
worked, at least on the last iteration, but when I access the object usingMsgBox
in the loop, it seems to fail.My ultimate goal is to iterate through a collection of objects, adding them one by one to the script engine, and executing user-defined VBScript code. The user code handles each object one at a time and expects the object to be named the same. I am open to alternative approaches but the current one worked well with the old MS ScriptControl and its
Reset
method.