How do I access JavaScript this from ScriptSharp?

2019-08-08 08:52发布

I'm trying to do the following.

var handler = e => { handle(); item.Unbind("event", this); }
item.Bind("event", handler);

In JavaScript this would properly work, but ScriptSharp replaces JavaScript's this with reference to the instance of class containing method with that code. How do I avoid this behavior and get a reference to the lambda from the lambda itself?

标签: this script#
1条回答
Emotional °昔
2楼-- · 2019-08-08 09:11

Here's how you could do it (assuming Bind takes a delegate with the signature of an Action):

SomeObject item = ...;
Action handler = null;

handler = delegate() {
   // do something ... eg. call Handle();
   item.Unbind("event", handler);
};
item.Bind("event", handler);

Also, see this question: How to write a function in script# to be called with any object as this, not just with the instance of the class in which it is defined? for a technique for writing code that generates a "this" reference in script.

查看更多
登录 后发表回答