0

使用“经典”方法实现,我通常像这样执行 BeginInvoke:

private delegate void FooDelegate();
public void Foo()
{
  if(InvokeRequired)
  {
    BeginInvoke(new FooDelegate(Foo));
    return;
  }

  // Do what you want here
}

当方法是显式接口成员声明时,如何做同样的事情?像:

public void IFace.Foo()
{
  // Need to BeginInvoke here
}

这不起作用:

private delegate void FooDelegate();
public void IFace.Foo()
{
  if(InvokeRequired)
  {
    BeginInvoke(new FooDelegate(IFace.Foo));
    return;
  }

  // Do what you want here
}
4

2 回答 2

1

您必须先转换thisIFace

var iface = (IFace)this;
BeginInvoke(new FooDelegate(iface.Foo));
于 2012-04-06T07:28:55.990 回答
0

因为您的实现是显式的,所以无法通过类实例访问 Foo 方法。仅通过接口实例。这意味着您需要将实例this转换为IFace实例。然后你可以将该方法传递给BeginInvoke

var asIFace = (IFace)this;
BeginInvoke(asIFace.Foo));
于 2012-04-06T07:41:39.010 回答