简体   繁体   English

这个代理调用在这行代码(C#)中做了什么?

[英]What is this delegate call doing in this line of code (C#)?

This is from an example accompanying the agsXMPP .Net assembly. 这是来自agsXMPP .Net程序集的示例。 I've read up on delegates, but am not sure how that fits in with this line of code (which waits for the logon to occur, and then sends a message. I guess what I'm looking for is an understanding of why delegate(0) accomplishes this, in the kind of simple terms I can understand. 我已经阅读了代理,但我不确定这个代码行是如何适应的(等待登录发生,然后发送消息。我想我正在寻找的是理解为什么delegate(0)用我能理解的简单术语来实现这一点。

xmpp.OnLogin += delegate(object o) { 
    xmpp.Send(new Message(new Jid(JID_RECEIVER), 
    MessageType.chat, 
    "Hello, how are you?")); 
};

It's exactly the same as 它完全一样

xmpp.OnLogin += EventHandler(MyMethod);

Where MyMethod is MyMethod在哪里

public void MyMethod(object o) 
{ 
    xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); 
}

As Abe noted, this code is creating an anonymous function. 正如Abe所说,这段代码正在创建一个匿名函数。 This: 这个:


xmpp.OnLogin += delegate(object o) 
   { 
      xmpp.Send(
         new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); 
   };

would have been accomplished as follows in older versions of .Net (I've excluded class declarations and such, and just kept the essential elements): 在旧版本的.Net中已经完成如下(我已经排除了类声明等等,只保留了基本元素):


delegate void OnLoginEventHandler(object o);

public void MyLoginEventHandler(object o)
{
      xmpp.Send(
         new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?")); 
}

[...]

xmpp.OnLogin += new OnLoginEventHandler(MyLoginEventHandler);

What you're doing in either case is associating a method of yours to run when the xmpp OnLogin event is fired. 在这两种情况下,您正在做的是将您的方法与xmpp OnLogin事件触发时的运行相关联。

OnLogin on xmpp is probably an event declared like this : OnLogin on xmpp可能是一个声明如下的事件:

public event LoginEventHandler OnLogin;

where LoginEventHandler is as delegate type probably declared as : 其中LoginEventHandler作为委托类型可能声明为:

public delegate void LoginEventHandler(Object o);

That means that in order to subscribe to the event, you need to provide a method (or an anonymous method / lambda expression ) which match the LoginEventHandler delegate signature. 这意味着,为了订阅事件,您需要提供与LoginEventHandler委托签名匹配的方法(或匿名方法 / lambda表达式 )。

In your example, you pass an anonymous method using the delegate keyword: 在您的示例中,您使用delegate关键字传递匿名方法:

xmpp.OnLogin += delegate(object o)
                { 
                    xmpp.Send(new Message(new Jid(JID_RECEIVER), 
                              MessageType.chat,
                              "Hello, how are you?")); 
                };

The anonymous method matches the delegate signature expected by the OnLogin event (void return type + one object argument). 匿名方法匹配OnLogin事件所期望的委托签名(void返回类型+一个对象参数)。 You could also remove the object o parameter leveraging the contravariance , since it is not used inside the anonymous method body. 您还可以删除利用逆变量object o参数,因为它不在匿名方法体内使用。

xmpp.OnLogin += delegate
                { 
                    xmpp.Send(new Message(new Jid(JID_RECEIVER), 
                              MessageType.chat,
                              "Hello, how are you?")); 
                };

The delegate(object o){..} tells the compiler to package up whatever is inside the brackets as an object to be executed later, in this case when OnLogin is fired. delegate(object o){..}告诉编译器将括号内的任何内容打包为稍后要执行的对象,在这种情况下触发OnLogin时。 Without the delegate() statement, the compiler would think you are tying to execute an action in the middle of an assignemnt statement and give you errors. 如果没有delegate()语句,编译器会认为您正在尝试在assignemnt语句中执行操作并给出错误。

That is creating an anonymous function. 那就是创建一个匿名函数。 This feature was introduced in C# 2.0 此功能是在C#2.0中引入的

It serves as an anonymous method, so you don't need to declare it somewhere else. 它用作匿名方法,因此您无需在其他位置声明它。 It's very useful. 这非常有用。

What it does in that case is to attach that method to the list of actions that are triggered because of the onLogin event. 在这种情况下,它的作用是将该方法附加到由于onLogin事件而触发的操作列表。

Agreed with Abe, this is an anonymous method. 同意安倍,这是一个匿名的方法。 An anonymous method is just that -- a method without a name, which can be supplied as a parameter argument. 匿名方法就是 - 没有名称的方法,可以作为参数参数提供。

Obviously the OnLogin object is an Event; 显然,OnLogin对象是一个事件; using an += operator ensures that the method specified by the anonymous delegate above is executed whenever the OnLogin event is raised. 使用+ =运算符可确保在引发OnLogin事件时执行上述匿名委托指定的方法。

Basically, the code inside the {} will run when the "OnLogin" event of the xmpp event is fired. 基本上,当触发xmpp事件的“OnLogin”事件时,{}内的代码将运行。 Based on the name, I'd guess that event fires at some point during the login process. 根据名称,我猜这个事件会在登录过程中的某个时刻触发。

The syntax: 语法:

delegate(object o) { statements; }

is a called an anonymous method. 是一种被称为匿名的方法。 The code in your question would be equivilent to this: 您问题中的代码与此无关:

public class MyClass
{
  private XMPPObjectType xmpp;
  public void Main()
  {
    xmpp.OnLogin += MyMethod;
  }
  private void MyMethod(object o)
  {
    xmpp.Send(new Message(new Jid(JID_RECEIVER), MessageType.chat, "Hello, how are you?"));
  }
}

You are subscribing to the OnLogin event in xmpp. 您正在订阅xmpp中的OnLogin事件。

This means that when xmpp fires this event, the code inside the anonymous delegate will fire. 这意味着当xmpp触发此事件时,匿名委托内的代码将触发。 Its an elegant way to have callbacks. 它是一种优雅的回调方式。

In Xmpp, something like this is going on: 在Xmpp中,这样的事情正在发生:

   // Check to see if we should fire the login event
   // ALso check to see if anything is subscribed to OnLogin 
   // (It will be null otherwise)
   if (loggedIn && OnLogin != null)
   {
       // Anyone subscribed will now receive the event.
       OnLogin(this);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM