是否可以将“这个”设置为在匿名函数中?

发布于 2024-09-30 16:54:59 字数 412 浏览 3 评论 0原文

我有一个函数,

public SharpQuery Each(Action<int, HtmlNode> function)
{
    for (int i = 0; i < _context.Count; ++i)
        function(i, _context[i]);
    return this;
}

它为上下文的每个元素调用传入的函数。是否可以在 Action中设置“this”所指的内容?函数?

例如,

sharpQuery.Each((i, node) => /* `this` refers to an HtmlNode here */);

I've got a function,

public SharpQuery Each(Action<int, HtmlNode> function)
{
    for (int i = 0; i < _context.Count; ++i)
        function(i, _context[i]);
    return this;
}

Which calls the passed in function for each element of the context. Is it possible to set what "this" refers to inside Action<int, HtmlNode> function?

For example,

sharpQuery.Each((i, node) => /* `this` refers to an HtmlNode here */);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初吻给了烟 2024-10-07 16:54:59

稍微改变一下功能,就可以达到想要的效果。

public SharpQuery Each(Action<MyObject, int, HtmlNode> function)
{
    for (int i = 0; i < _context.Count; ++i)
        function(this, i, _context[i]);
    return this;
}

然后您可以像这样编写函数调用:

sharpQuery.Each((self, i, node) => /* do something with `self` which is "this" */);

注意:但是,匿名函数只能访问公共成员。如果匿名函数是在类中定义的,则它将像往常一样访问受保护的成员和私有成员。

例如,

class MyObject
{
    public MyObject(int i)
    {
        this.Number = i;
    }

    public int Number { get; private set; }
    private int NumberPlus { get { return Number + 1; } }

    public void DoAction(Action<MyObject> action)
    {
        action(this);
    }

    public void PrintNumberPlus()
    {
        DoAction(self => Console.WriteLine(self.NumberPlus));  // has access to private `NumberPlus`
    }
}

MyObject obj = new MyObject(20);
obj.DoAction(self => Console.WriteLine(self.Number));     // ok
obj.PrintNumberPlus();                                    // ok
obj.DoAction(self => Console.WriteLine(self.NumberPlus)); // error

With a slight change in the function, you can achieve the desired effect.

public SharpQuery Each(Action<MyObject, int, HtmlNode> function)
{
    for (int i = 0; i < _context.Count; ++i)
        function(this, i, _context[i]);
    return this;
}

Then you could write your function call like so:

sharpQuery.Each((self, i, node) => /* do something with `self` which is "this" */);

Note: The anonymous function will only have access to public members however. If the anonymous function was defined within the class, it will have access to protected and private members as usual.

e.g.,

class MyObject
{
    public MyObject(int i)
    {
        this.Number = i;
    }

    public int Number { get; private set; }
    private int NumberPlus { get { return Number + 1; } }

    public void DoAction(Action<MyObject> action)
    {
        action(this);
    }

    public void PrintNumberPlus()
    {
        DoAction(self => Console.WriteLine(self.NumberPlus));  // has access to private `NumberPlus`
    }
}

MyObject obj = new MyObject(20);
obj.DoAction(self => Console.WriteLine(self.Number));     // ok
obj.PrintNumberPlus();                                    // ok
obj.DoAction(self => Console.WriteLine(self.NumberPlus)); // error
甜味超标? 2024-10-07 16:54:59

不。

嗯,是的,如果 Action 是在“this”可用并绑定在闭包中的范围内创建的,但显然:不。

传递所有需要的信息或确保它在操作本身中捕获/可用。还有其他黑客,如线程局部变量等。最好避免。

No.

Well, yes, if the Action was created in such a scope where 'this' was available and bound in a closure -- but transparently: no.

Pass in all needed information or make sure it's captured/available in the Action itself. There are other hacks like thread-locals, etc. Best avoided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文