microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.77k stars 148 forks source link

Question about property object assignment #555

Closed sergioamm17 closed 10 months ago

sergioamm17 commented 10 months ago

Currently I have an object reference inside the script For example IM.Reference or IM.Origin If I send a value from the code, within the script we can access the value, However, if we want to assign a value from the script we can do this: IM.Reference = "somthing" it works because is a string or primitive type IM.Origin = new Namespace.MyObject() this doesn't work as expected, even if I use the HostFunction include in the library. This does not work either IM.Origin = host.newObj(Namespace.MyObject);

I need help to create a new instance or object of my type and then assign properties to the object. My property Origin is public and we have implemented the get and set methods to see if it works but never hit the debugger.

The current error I'm getting is: Invalid property assignment

ClearScriptLib commented 10 months ago

Hi @sergioamm17,

We're assuming that IM is a host object. If that's correct, how are the Reference and Origin properties defined in managed code?

Also, what is Namespace.MyObject? If that's a host object or type, how are you exposing it to the script engine?

Thanks!

sergioamm17 commented 10 months ago

Yes, the IM is a host object.

This is the Origin property private MyCustomObject _origin; public MyCustomObject Origin { get { return _origin; } set { _origin = value; } } And the Reference property public string Reference { get { return Convert.ToString(Entity.iProperties[MemberName.Reference]); } set { Entity.iProperties[MemberName.Reference] = value; } }

Reference is working as expected, however, the Origin is not working at all. I think the reason is because is an complex type (my custom object)

Namespace.MyObject is the namespace and name of my custom class, for example MyCustomObject according to the shared example. I already registered my custom type using Engine.AddHostType("MyTypes", typeof(Namespace.MyCustomObject)); I want to set the new value for the Origin property from the script.

ClearScriptLib commented 10 months ago

Hello @sergioamm17,

I already registered my custom type using Engine.AddHostType("MyTypes", typeof(Namespace.MyCustomObject)); I want to set the new value for the Origin property from the script.

Given that AddHostType call, the JavaScript expression new MyTypes() should create a MyCustomObject instance (assuming that MyCustomObject has a parameterless constructor).

Can you verify that?

sergioamm17 commented 10 months ago

Yes, my class MyCustomObject already has a parameterless constructor.

I made a couple of more tests and it looks the type of the property Origin is not registered at all. This should be why I can not assign a new object to the property, even if I create another object type, it's not the same as the property and that throws the exception.

So far, I need to fix a namespaces error, to register succesfully the Origin Type.

This is a shared project between a big team, that's why I'm trying to fix this kind of programming errors. Thanks for your help, you made me understand the issue.

ClearScriptLib commented 10 months ago

Please reopen this issue if you have additional questions or comments about this topic. Thank you!