I am having trouble figuring out how to program delegate method calls across classes in C#. I am coming from the world of Objective-C, which may be confusing me. In Objective-C, I can assign a delegate object inside a child class, to be the parent class (I.e., childViewcontroller.delegate = self;
). Then I can to fire a method in the delegate class by using:
我无法弄清楚如何在C#中跨类编写委托方法调用。我来自Objective-C的世界,这可能让我感到困惑。在Objective-C中,我可以在子类中分配一个委托对象,作为父类(即,childViewcontroller.delegate = self;)。然后我可以使用以下命令在委托类中触发方法:
if([delegate respondsToSelector:@selector(methodName:)]) {
[delegate methodName:parametersgohere];
}
However, I can't figure out how to do this in C#. I've read a bit about C# delegates in general (for example, here), but I'm still stuck.
但是,我无法弄清楚如何在C#中做到这一点。我已经阅读了一些关于C#代理的一般信息(例如,这里),但我仍然卡住了。
Are there any examples that explain this?
有没有例子可以解释这个?
Here is my scenario in full: I have classA which instantiates an instance of classB. ClassB fires a method (which call a web service), and upon response, I'd like to fire a method in classA.
这是我的场景:我有classA实例化classB的一个实例。 ClassB触发一个方法(调用Web服务),并在响应时,我想在classA中触发一个方法。
Any 'Hello World' types of tutorials out there that might explain the very basics of this?
任何“Hello World”类型的教程都可以解释这个基础知识吗?
2 个解决方案
#1
4
A delegate is an object that points to a method, be it a static or instance method. So for your example, you would just use the event model:
委托是指向方法的对象,无论是静态方法还是实例方法。因此,对于您的示例,您只需使用事件模型:
class Caller {
public void Call() {
new Callee().DoSomething(this.Callback); // Pass in a delegate of this instance
}
public void Callback() {
Console.WriteLine("Callback called!");
}
}
class Callee {
public void DoSomething(Action callback) {
// Do stuff
callback(); // Call the callback
}
}
...
new Caller().Call(); // Callback called!
The Caller
instance passes a delegate to the Callee
instance's DoSomething
method, which in turn calls the pointed-to method, which is the Callback
method of the Caller
instance.
Caller实例将委托传递给Callee实例的DoSomething方法,该方法又调用指向的方法,该方法是Caller实例的Callback方法。
#2
4
In C# what I think you are looking for are called events. They are a language feature that allows a class instance to expose a public delegate in a way that other class instances can subscribe to. Only the exposing class is allowed to raise the event.
在C#中,我认为您正在寻找的是被称为事件。它们是一种语言功能,允许类实例以其他类实例可以订阅的方式公开公共委托。只允许暴露类引发事件。
In your example:
在你的例子中:
public class ClassB {
// Note the syntax at the end here- the "(s, e) => { }"
// assigns a no-op listener so that you don't have to
// check the event for null before raising it.
public event EventHandler<MyEventArgs> MyEvent = (s, e) => { }
public void DoMyWork() {
// Do whatever
// Then notify listeners that the event was fired
MyEvent(this, new MyEventArgs(myWorkResult));
}
}
public class ClassA {
public ClassA(ClassB worker) {
// Attach to worker's event
worker.MyEvent += MyEventHandler;
// If you want to detach later, use
// worker.MyEvent -= MyEventHandler;
}
void MyEventHandler(Object sender, MyEventArgs e) {
// This will get fired when B's event is raised
}
}
public class MyEventArgs : EventArgs {
public String MyWorkResult { get; private set; }
public MyEventArgs(String myWorkResult) { MyWorkResult = myWorkResult; }
}
Note that the above will be synchronous. My understanding is that Objective-C delegates are all Actor pattern, so they are asynchronous. To make the above asynch, you'll need to delve into threading (probably want to google "C# Thread pool").
请注意,上述内容将是同步的。我的理解是Objective-C委托都是Actor模式,所以它们是异步的。要进行上述异步操作,您需要深入研究线程(可能想要谷歌“C#线程池”)。
#1
4
A delegate is an object that points to a method, be it a static or instance method. So for your example, you would just use the event model:
委托是指向方法的对象,无论是静态方法还是实例方法。因此,对于您的示例,您只需使用事件模型:
class Caller {
public void Call() {
new Callee().DoSomething(this.Callback); // Pass in a delegate of this instance
}
public void Callback() {
Console.WriteLine("Callback called!");
}
}
class Callee {
public void DoSomething(Action callback) {
// Do stuff
callback(); // Call the callback
}
}
...
new Caller().Call(); // Callback called!
The Caller
instance passes a delegate to the Callee
instance's DoSomething
method, which in turn calls the pointed-to method, which is the Callback
method of the Caller
instance.
Caller实例将委托传递给Callee实例的DoSomething方法,该方法又调用指向的方法,该方法是Caller实例的Callback方法。
#2
4
In C# what I think you are looking for are called events. They are a language feature that allows a class instance to expose a public delegate in a way that other class instances can subscribe to. Only the exposing class is allowed to raise the event.
在C#中,我认为您正在寻找的是被称为事件。它们是一种语言功能,允许类实例以其他类实例可以订阅的方式公开公共委托。只允许暴露类引发事件。
In your example:
在你的例子中:
public class ClassB {
// Note the syntax at the end here- the "(s, e) => { }"
// assigns a no-op listener so that you don't have to
// check the event for null before raising it.
public event EventHandler<MyEventArgs> MyEvent = (s, e) => { }
public void DoMyWork() {
// Do whatever
// Then notify listeners that the event was fired
MyEvent(this, new MyEventArgs(myWorkResult));
}
}
public class ClassA {
public ClassA(ClassB worker) {
// Attach to worker's event
worker.MyEvent += MyEventHandler;
// If you want to detach later, use
// worker.MyEvent -= MyEventHandler;
}
void MyEventHandler(Object sender, MyEventArgs e) {
// This will get fired when B's event is raised
}
}
public class MyEventArgs : EventArgs {
public String MyWorkResult { get; private set; }
public MyEventArgs(String myWorkResult) { MyWorkResult = myWorkResult; }
}
Note that the above will be synchronous. My understanding is that Objective-C delegates are all Actor pattern, so they are asynchronous. To make the above asynch, you'll need to delve into threading (probably want to google "C# Thread pool").
请注意,上述内容将是同步的。我的理解是Objective-C委托都是Actor模式,所以它们是异步的。要进行上述异步操作,您需要深入研究线程(可能想要谷歌“C#线程池”)。