This project has moved and is read-only. For the latest updates, please go here.

Enriching an object in script = object disposed exception?

Mar 28, 2014 at 11:09 AM
Edited Mar 28, 2014 at 11:10 AM

Probably doing something stupid, but in my host app I have an object, which I pass into the script engine. The script then adds some new properties to it (it's a dynamic), then the engine is disposed and my host object contains these new properties.

However, in my script, if I add a new property to the object like this;
theObject.someNewProperty = {
     innerProperty1 = 10,
     innerProperty2 = "Hello world"
If I then access theObject.someNewProperty within my host, after the engine has disposed, I get an ObjectDisposedException.

Any thoughts / assistance appreciated.

Mar 28, 2014 at 1:54 PM
Hi Tony,

Your code above creates a property (named "someNewProperty") whose value is a script object.

Fundamentally, a script object is not a standalone object; it can be considered a sort of reference into a script engine's in-memory state. Accessing the properties of a script object requires the services of the script engine that created it.

In your scenario, consider using a host object instead. For example:
// C#
engine.AddHostType("ExpandoObject", typeof(ExpandoObject));
and then:
// JavaScript
theObject.someNewProperty = new ExpandoObject();
theObject.someNewProperty.innerProperty1 = 10;
theObject.someNewProperty.innerProperty2 = 'Hello world';
Good luck!
Apr 2, 2014 at 11:45 AM
Thanks kindly - that's sorted it :)