如何在 ClearScript 下使用 V8 更新 System.Data datarow 中的列?



我正在使用 V8ScriptEngine

v8 = new V8ScriptEngine(V8ScriptEngineFlags.DisableGlobalMembers);

并将"System.Data"命名空间公开到 ClearScript 环境中。

var htc = new HostTypeCollection();
foreach (var assembly in new string[] { "mscorlib", "System", "System.Core", "System.Data" })
{
htc.AddAssembly(assembly);
}
...
v8.AddHostObject("CS", htc);

然后,我从Microsoft中获取了一个System.Data示例,并尝试使其适应ClearScript/V8环境

const DataTable = CS.System.Data.DataTable;
const DataColumn = CS.System.Data.DataColumn;
const DataRow = CS.System.Data.DataRow;
const table = new DataTable("Product");
// Create a DataColumn and set various properties.
const column = new DataColumn();
column.DataType = CS.System.Type.GetType("System.Decimal");
column.AllowDBNull = false;
column.Caption = "Price";
column.ColumnName = "Price";
column.DefaultValue = 25;
// Add the column to the table.
table.Columns.Add(column);
// Add 10 rows and set values.
let row;
for (let i = 0; i < 10; i++) {
row = table.NewRow();
row["Price"] = i + 1;
// Be sure to add the new row to the
// DataRowCollection.
table.Rows.Add(row);
}

我在这一点上的困难是row["Price"] = i + 1在 C# 上下文中运行良好,但在 ClearScript/V8 中表现不佳,抛出

Error: Object has no suitable property or field named 'Price'
at Script:24:18 ->     row["Price"] = i + 1;

所以问题是,如何更新一行中的列?我在早期通过在 C# 端编写一个帮助程序函数来解决这个问题。这仍然是最好的解决方案吗?

代替row["Price"] = i + 1,使用row.Item.set("Price", i + 1)。有关解释,请参阅此处。

相关内容

最新更新