-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you set a .NET enum from JavaScript? #156
Comments
Hi @JohnLudlow, The issue here is that There are several ways to make the host.AddHostType(HostItemFlags.PrivateAccess, typeof(Status)); See Good luck! |
Dang how did I miss that? Thanks |
Please reopen this issue if you have additional questions or concerns about this topic. Thank you! |
Will do, but I think that answers it in terms of enums. Thanks |
I noticed that I wasn't able to find a function to set an enum from a plain number being passed in, which surprised me. Byte, int32, int64, nullable, string, ScriptObject... so much just works! Since an enum is backed by an int32 by default, I thought it would have been simple to use just a number. |
Hi @Brain2000,
The behavior here is defined by C#, which doesn't implicitly convert integers to enums. Consider this simple example: public enum Kingdom { Plant, Animal };
public class Being { public Kingdom Kingdom; } And then: engine.AddHostType(typeof(Kingdom));
engine.Script.me = new Being(); To assign an integer value to engine.Script.host = new HostFunctions();
engine.Execute(@"
me.Kingdom = Kingdom.Animal; // OK
me.Kingdom = host.cast(Kingdom, 1); // OK; explicit conversion
me.Kingdom = 0; // OK; SPECIAL CASE: zero is implicitly convertible to all enums
me.Kingdom = 1; // ERROR
"); Good luck! |
Is there a way to get the integer value of an enum on the script side? I can get the name using Ideally the |
Hello @EtienneLaneville,
Sure. You can use engine.AddHostObject("host", new HostFunctions());
engine.AddHostType(typeof(Int32));
engine.Execute("MsgBox host.cast(Int32, Kingdom.Animal)"); Another possibility is expose a managed function that performs the conversion: engine.Script.EnumToInt = new Func<Enum, int>(Convert.ToInt32);
engine.Execute("MsgBox EnumToInt(Kingdom.Animal)");
Right. Those functions are designed for a different purpose – namely, to allow script code to specify the exact numeric type of a host method argument in order to avoid ambiguity in specific scenarios.
Unfortunately, VBScript's Cheers! |
Instead of using |
A delegate is an object, so engine.AddHostType(typeof(Kingdom));
engine.AddHostType(typeof(Console));
engine.AddHostObject("EnumToInt", new Func<Enum, int>(Convert.ToInt32));
engine.Execute("Console.WriteLine(EnumToInt(Kingdom.Animal))"); The following all do pretty much the same thing: engine.AddHostObject("foo", obj);
engine.Script.foo = obj;
engine.Script["foo"] = obj;
Good luck! |
How can I set a .NET enum from JavaScript. The common use case is an object with a property of an enum type or method which takes an enum, but here is a simplified (slightly contrived) use case: setting an enum variable.
In this case,
b
andhost.Script.status
are undefined. How can we create the correct value from the enum here?The text was updated successfully, but these errors were encountered: