// DynamicObject 取值/赋值/分录遍历示例 // 适用插件:Form/Operation/Validator/Report // 依赖 API:DynamicObject, DynamicObjectCollection // 相关检查:属性深路径访问推荐判空,金额推荐使用 decimal 避免 float/double using System; using Kingdee.BOS.Orm.DataEntity; public class DynamicObjectSample { public void DemonstrateDynamicObject(DynamicObject bill) { // 表头取值 string billNo = bill["billno"] as string; long id = Convert.ToInt64(bill["id"] ?? 0); decimal amount = Convert.ToDecimal(bill["amount"] ?? 0); // 基础资料字段取值(返回 DynamicObject,需判空) DynamicObject supplier = bill["supplier"] as DynamicObject; if (supplier != null) { string supplierName = supplier["Name"] as string; long supplierId = Convert.ToInt64(supplier["Id"]); } // 分录遍历 string entryKey = "POOrderEntry"; DynamicObjectCollection entryRows = bill[entryKey] as DynamicObjectCollection; if (entryRows != null) { foreach (DynamicObject row in entryRows) { long entryId = Convert.ToInt64(row["id"] ?? 0); decimal qty = Convert.ToDecimal(row["qty"] ?? 0); decimal price = Convert.ToDecimal(row["price"] ?? 0); // 分录基础资料取值 DynamicObject material = row["material"] as DynamicObject; if (material != null) { string materialNumber = material["Number"] as string; } } } // 赋值 bill["amount"] = 1000.50m; bill["description"] = "测试单据"; // 分录新增(通过 Model 在表单插件中操作) // this.Model.CreateNewEntryRow(entryKey); } }