|
Jint doesn't have an explicit method to control the marshaling, but it's not hard to make some customization. Actually marshalling consists of several simple steps:
1. Creating a regular object
2. Setting the internal value to the CLR instance
3. Propagating CLR properties to the js object
3. Specify a proper prototype which is also a regular object which contains a wrappers around CLR methods
Steps 2 and 3 can be made by applying a CLR constructor, i.e. My.Namespace.Foo.apply(jsobject,...). A new CLR instance will be created an assigned to jsobject and all public native properties will be propagated.
Here more complete example
function CustomFoo() {
My.Namespace.Foo.apply(this);
};
CustomFoo.prototype.testMe = My.Namespace.Foo.TestMethod;
var o = new CustomFoo();
o.testMe(); // will call My.Namespace.Foo.TestMethod
Same results may be achieved in the CLR:
1. You need to create JsObject with two parameters: a CLR instance which you are marshalling, and a desired prototype which will define available methods
2. Use NativeConstrutor.SetupNativeProperties on the created JsObject to populate a native properties (use Marshaller.MarshalType(type) to get a corresponding NativeConstructor)
|