【问题标题】:Arguments to delegate.BeginInvoke are copies or references?delegate.BeginInvoke 的参数是副本还是引用?
【发布时间】:2011-02-10 18:52:01
【问题描述】:

哪里有关于 BeginInvoke 参数处理的文档?

如果我有一个接受对象作为参数的委托(包装了我的处理函数),该对象是否会被异步调用的处理函数复制或引用?

delegate void MyDelegate(SomeObject obj);

// later on:
// invoke the delegate async'ly:
new MyDelegate(StaticClass.HandlerFunc).BeginInvoke(objInstance, null, null);
// alter the object:
objInstance.SomeProperty = newValue;

// function:
public static void HandlerFunc(SomeObject obj) {
   // is it a possible race condition to read SomeProperty:
   if(obj.SomeProperty == oldValue) {
      // will possibly never enter?
   }
   // ... etc.
}

【问题讨论】:

    标签: .net delegates pass-by-reference


    【解决方案1】:

    方法获取对象的引用。

    对象不会在 .NET 中复制,除非您专门创建副本。

    【讨论】:

      猜你喜欢
      • 2011-08-30
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 2014-05-26
      相关资源
      最近更新 更多