2017-07-01 9 views
1

Javascript変数を(ClearScript.V8を使用して)インスタンス化し、C#で更新をキャッチしてデータベースの値を更新できる方法はありますか?ClearScript.V8で更新イベントを取得するにはどうすればよいですか?

私はこのように、オブジェクトを使用する場合それは動作します:

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コードは

代わりの

variable.valueのようなオブジェクトを使用する必要があります単純に

可変

答えて

1

あなたは、あなたのスマート変数に呼び出すアクセサで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"); 
関連する問題