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.
enum Status
{
Succeeded,
Failed,
InProgress,
}
using (var host = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging, 9002))
{
host.AddHostType(typeof (Status));
host.Script.status = Status.Succeeded;
var a = (host.Evaluate("status"));
var b = (host.Evaluate("status = Status.Failed"));
}
In this case, b and host.Script.status are undefined. How can we create the correct value from the enum here?
Hi @JohnLudlow,
The issue here is that Status isn't a public type. ClearScript strictly enforces member visibility even for explicitly exposed types.
There are several ways to make the Status fields visible to script code. For example, you could make Status a public type. Another possibility is to expose it with the private access option:
C#
host.AddHostType(HostItemFlags.PrivateAccess, typeof(Status));
See ScriptEngine.AccessContext for another alternative.
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