Vb.net代码转C#
Class DChatServer
' Declare a multicast (because it has no return type) delegate type
Public Delegate Sub OnMsgArrived(ByVal message As String)
' Declare a reference to an OnMsgArrived delegate
' Note: this field is private to prevent clients from removing or invoking delegates
Private Shared mOnMsgArrived As OnMsgArrived
' The following method is used to provide clients a public method to add delegates to the onMsgArrived 's invocation list
Public Shared Sub ClientConnect(ByVal aDelegate As OnMsgArrived)
DChatServer.mOnMsgArrived = _
OnMsgArrived.Combine(DChatServer.mOnMsgArrived, aDelegate)
End Sub
Public Shared Sub ClientDisconnect(ByVal aDelegate As OnMsgArrived)
OnMsgArrived.Remove(DChatServer.mOnMsgArrived, aDelegate)
End Sub
' optional SendMsg helper method, not required by the lab
Public Shared Sub SendMsg(ByVal msg As String)
' Send message to ALL clients
SendMsg(msg, Nothing)
End Sub
Public Shared Sub SendMsg(ByVal msg As String, ByVal excludeClient As Object)
' Send message to all clients except 'excludeClient '
If (excludeClient Is Nothing) Then
mOnMsgArrived.Invoke(msg)
Else
Dim DelegateList As [Delegate]() = mOnMsgArrived.GetInvocationList()
Dim i As Integer
For i = 0 To (DelegateList.Length - 1)
If Not (DelegateList(i).Target Is excludeClient) Then
CType(DelegateList(i), OnMsgArrived).Invoke(msg)