如何在 ClearScript.V8 中捕获更新事件?



有没有办法实例化Javascript变量(使用ClearScript.V8(并在C#中捕获更新,以便我可以更新数据库中的值?

当我使用对象时它有效,如下所示:

public class SmartBoolean : ISmartVariable
{
private Boolean _simulation;
private Guid UserId;
private long userKeyId;
private long variableKeyId;
private string Name;
private Boolean _value;
public Boolean value
{
get {
return _value;
}
set {
_value = value;
if (!_simulation)
{
if (value)
new SmartCommon().SetTrue(userKeyId, variableKeyId);
else
new SmartCommon().SetFalse(userKeyId, variableKeyId);
}
}
}
public SmartBoolean(Guid UserId, long userKeyId, long variableKeyId, string Name, Boolean value, Boolean simulation)
{
this.Name = Name;
this._value = value;
this.UserId = UserId;
this.userKeyId = userKeyId;
this.variableKeyId = variableKeyId;
this._simulation = simulation;
}
public Boolean toggle()
{
this.value = !this._value;
return this._value;
}
}

但是Javascript代码需要使用一个对象,例如

变量.值

而不是简单地

变量

.

您可以使用调用智能变量的访问器定义 JavaScript 全局属性:

dynamic addSmartProperty = engine.Evaluate(@"
(function (obj, name, smartVariable) {
Object.defineProperty(obj, name, {
enumerable: true,
get: function () { return smartVariable.value; },
set: function (value) { smartVariable.value = value; }
});
})
");
var smartVariable = new SmartBoolean(...);
addSmartProperty(engine.Script, "variable", smartVariable);
engine.Execute("variable = true");

相关内容

  • 没有找到相关文章

最新更新