首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Vb.net代码转C#解决方案

2012-01-10 
Vb.net代码转C#ClassDChatServerDeclareamulticast(becauseithasnoreturntype)delegatetypePublicDelegate

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)


                                End   If
                        Next
                End   If
        End   Sub
        '   Private   to   prevent   instances   of   this   type   from   being   instantiated.
        Private   Sub   New()
        End   Sub
End   Class
如何将   CType(DelegateList(i),   OnMsgArrived).Invoke(msg)转为C#


[解决办法]
Class DChatServer
public delegate void OnMsgArrived(string message);
Private Shared mOnMsgArrived As OnMsgArrived
public static void ClientConnect(OnMsgArrived aDelegate)
{
DChatServer.mOnMsgArrived =OnMsgArrived.Combine(DChatServer.mOnMsgArrived, aDelegate);
}
Public static void ClientDisconnect(OnMsgArrived aDelegate)
{
OnMsgArrived.Remove(DChatServer.mOnMsgArrived, aDelegate);
}
Public static void SendMsg(string msg)
{
SendMsg(msg, Nothing);
}
public static void SendMsg(String msg, Object excludeClient)
if(excludeClient==null)
mOnMsgArrived.Invoke(msg);
else
{
Delegate[] DelegateList = mOnMsgArrived.GetInvocationList();
int i;
for(i = 0;i <=(DelegateList.Length - 1);++i)
{
if(!(DelegateList[i].Target Is excludeClient)
CType(DelegateList(i), OnMsgArrived).Invoke(msg);

}
}
End Sub
private DChatServer(){}
End Class

[解决办法]
public delegate void OnMsgArrived(string message);
Private Shared mOnMsgArrived As OnMsgArrived
————————————————————
delegate 不是这样翻译的。好像VB.net用delegate的时候很方便。

delegate void delegate_name(参数名);
public void method1()
{
delegate_name delg1 = new delegate_name(method2);
this.BeginInvoke(delg1,new object[] {参数名});
}

public void method2(参数名)
{
}

热点排行