如何在 Clearscript 中序列化已编译的 V8Script?

How do I serialize compiled V8Script in Clearscript?

我正在使用 ClearScript 编译一些 JavaScript,然后我想将其序列化以将其存储在 SQL 中。但是被标记为not Serializable,怎么办?

V8ScriptEngine engine = new V8ScriptEngine();
V8Script compiled = engine.Compile("var a = 'test'");
using (MemoryStream ms = new MemoryStream())
{
    new BinaryFormatter().Serialize(ms, compiled);
    string compiledString = Convert.ToBase64String(ms.ToArray());
}

我收到这个错误:

Additional information: Type 'Microsoft.ClearScript.V8.V8ScriptImpl' in Assembly 'ClearScriptV8-32, Version=5.4.6.0, Culture=neutral, PublicKeyToken=935d0c957da47c73' is not marked as serializable.

V8 编译脚本绑定到创建它的隔离实例,因此序列化它没有意义。您不能在不同的进程中重用它,甚至不能在同一进程中使用另一个隔离。还有更多信息 here and here