[英]How do I update a column in a System.Data datarow using V8 under ClearScript?
我正在使用 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# 端編寫一個幫助程序 function 來解決這個問題。 這仍然是最好的解決方案嗎?
而不是row["Price"] = i + 1
,使用row.Item.set("Price", i + 1)
。 請參閱此處以獲取解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.