C#中的代表,参考解决时间 [英] C# delegates, reference resolution time

查看:98
本文介绍了C#中的代表,参考解决时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于.NET代表一个简单的问题。说我有这样的事情:

I have a simple question about .net delegates. Say I have something like this:

    public void Invoke(Action<T> action)
    {
        Invoke(() => action(this.Value));
    }

    public void Invoke(Action action)
    {
        m_TaskQueue.Enqueue(action);
    }

第一个函数封装引用 THIS.VALUE 。在运行时,当第一,法泛型参数被调用,它将提供 THIS.VALUE 莫名其妙的第二个,但如何?这些进入了我的脑海里:

The first function encloses a reference to this.Value. During runtime, when the first, method with generic parameter gets called, it will provide this.Value somehow to the second one, but how? These came into my mind:

  • 拨打按值(结构) - 的电流值 THIS.VALUE 获取传递,因此,如果 m_TaskQueue 在5分钟后执行它,价值会不会在它的最近的状态,这将是不管它是什么时候第一次引用。
  • 拨打引用(引用类型) - 其最新的国家将执行行动中,但如果我改变引用< $ C C> THIS.VALUE $来执行行动之前,另一个参考,它仍然会指向旧的参考
  • 拨打的名字(均) - 其中 THIS.VALUE 时的动作被调用将被评估。我相信,在实际执行将举行一个参照然后评估在该委托的,因为有实际的执行过程中是名称的电话。
  • Call by value (struct) - the current value of this.Value gets passed, so if the m_TaskQueue executes it 5 minutes later, the value will not be in it's recent state, it will be whatever it was when first referencing.
  • Call by reference (reference type) - then the most recent state of Value will be referenced during execution of action but if I change this.Value to another reference before execution of action, it will still be pointing to the old reference
  • Call by name (both) - where this.Value will be evaluated when the action gets called. I believe the actual implementation would be holding a reference to this then evaluate Value on that during actual execution of delegate since there is no call by name.

我相信它会被调用的名字风格,但找不到任何文档,以便知道是否有一个良好定义的行为。这个类是像斯卡拉或二郎的演员,所以我需要它是线程安全的。我不想调用函数间接引用马上,将在一个安全的线程<$ C $完成C>这按对象 m_TaskQueue

I assume it would be Call by name style but could not find any documentation so wondering if it is a well defined behavior. This class is something like an Actor in Scala or Erlang so I need it to be thread safe. I do not want Invoke function to dereference Value immediately, that will be done in a safe thread for this object by m_TaskQueue.

感谢。

推荐答案

我来回答你的问题描述什么是code,我们实际上产生了这一点。我会重命名混淆的名称的其他Invoke方法;这是没有必要了解什么是怎么回事。

Let me answer your question by describing what code we actually generate for this. I'll rename your confusingly-named other Invoke method; it's not necessary to understanding what's going on here.

假设你说

class C<T>
{
  public T Value;
  public void Invoke(Action<T> action) 
  { 
      Frob(() => action(this.Value)); 
  } 
  public void Frob(Action action) 
  {  // whatever
  } 
}

编译器生成code,仿佛你已经实际写入:

The compiler generates code as though you had actually written:

class C<T>
{
  public T Value;

  private class CLOSURE
  {
     public Action<T> ACTION;
     public C<T> THIS;
     public void METHOD()
     {
       this.ACTION(this.THIS.Value);
     }
  }

  public void Invoke(Action<T> action) 
  { 
      CLOSURE closure = new CLOSURE();
      closure.THIS = this;
      closure.ACTION = action;
      Frob(new Action(closure.METHOD)); 
  } 
  public void Frob(Action action) 
  {  // whatever
  } 
}

这是否回答你的问题?

Does that answer your question?

这篇关于C#中的代表,参考解决时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆