سؤال

With a "classic" method implementation, I usually perform BeginInvoke like this:

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

  // Do what you want here
}

How to do the same thing when the method is an explicit interface member declaration? Like:

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

This does not work:

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

  // Do what you want here
}
هل كانت مفيدة؟

المحلول

You have to cast this to IFace first:

var iface = (IFace)this;
BeginInvoke(new FooDelegate(iface.Foo));

نصائح أخرى

Because your implementation is explicit, the method Foo cannot be accessed via the class instance. Only via the interface instance. This means you need to cast your instance this to an IFace instance. Then you can pass that method to BeginInvoke:

var asIFace = (IFace)this;
BeginInvoke(asIFace.Foo));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top