繁体   English   中英

VB.net 到 C# 之间的转换(新 EventHandler) 没有重载匹配委托

[英]Conversion between VB.net TO C# (new EventHandler) NO overload matches delegate

我是 C# 的新手,我需要将我的代码从 VB.NET 转换为 C#。

这是我的 VB.NET 代码

Private Sub Receive()
    Dim inp As NetworkStream
    Dim income As BinaryReader
    inp = TCP.GetStream
    income = New BinaryReader(inp)

    While (State = "Connected")
        Try
            msg = income.ReadChar
            Application.OpenForms(0).Invoke(New EventHandler(AddressOf prin))
        Catch ex As Exception
            Try
                Me.Det = False
                If TCpThread.IsAlive = True Then TCpThread.Abort()
                TCP.Close()
                msg = ex.Message
            Catch ex1 As Exception
                Application.OpenForms(0).Invoke(New EventHandler(AddressOf prin))
            End Try
            State = "IDLE"
        End Try
    End While
End Sub

Private Sub prin()
    Message &= msg
    Check(Message)
End Sub

它工作正常,我将其转换为:

private void Receive()
{
    NetworkStream inp = default(NetworkStream);
    BinaryReader income = default(BinaryReader);
    inp = TCP.GetStream();
    income = new BinaryReader(inp);

    while (State == "Connected")
    {
        try
        {
            msg = System.Convert.ToString(income.ReadChar());
            Application.OpenForms[0].Invoke(new EventHandler(prin));
        }
        catch (Exception ex)
        {
            try
            {
                this.Det = false;
                if (TCpThread.IsAlive == true)
                {
                    TCpThread.Abort();
                }
                TCP.Close();
                msg = ex.Message;
            }
            catch (Exception)
            {
                Application.OpenForms[0].Invoke(new EventHandler(prin));
            }
            State = "IDLE";
        }
    }
}

private void prin()
{
    Message += msg;
    Check(Message);
}

但我明白了

错误 1 'prin' 没有重载匹配委托 'System.EventHandler'

对于这一行Application.OpenForms[0].Invoke(new EventHandler(prin)); . 基于多线程,我需要调用 function 以便在我的线程运行时跟踪它。

我的错误是什么?

任何帮助将不胜感激。

利用

Application.OpenForms[0].Invoke(new Action(prin));

EventHandler类型表示具有两个参数(通用objectEventArgs对象)的委托。 您的方法prin与该描述不匹配。

如果您只想为您的方法创建一个委托,请使用与您的方法具有相同 arguments 的委托类型(在本例中为无)。 在这种情况下,这是Action委托。

Action 委托也应该首先在 VB 代码中使用:

Application.OpenForms(0).Invoke(New Action(AddressOf prin))

但是,在 VB 中使用EventHandler是有效的,因为 VB 中的AddressOf运算符并不是真正的类型安全的。

这里有一些 VB.NET 代码到 C# 转换。 有些工作,有些不工作

Invoke(AddressOf prin) ' doesn't compile
Invoke(Sub() prin())
Invoke(New Action(AddressOf prin))
Invoke(New NoParameterVoid(AddressOf prin))
Invoke(New EventHandler(AddressOf prin)) ' doesn't compile with Option Strict On (original code)
' ...
Delegate Sub NoParameterVoid() ' required for 4th item
Invoke(prin); // doesn't compile
Invoke(() => prin()); // doesn't compile
Invoke(new Action(prin));
Invoke(new NoParameterVoid(prin));
Invoke(new EventHandler(prin)); // doesn't compile
// ...
delegate void NoParameterVoid(); // required for 4th item

所以只有两个可以直接转换为 C# 代码的编译是

Invoke(New Action(AddressOf prin))
Invoke(New NoParameterVoid(AddressOf prin))

并且由于我们需要创建的Delegate Sub NoParameterVoid()Action()相同,因此在我看来,创建它没有意义。 因此,当尝试直接转换为 c# 时,唯一合理的选择是:

Invoke(New Action(AddressOf prin))
Invoke(new Action(prin));

如果您一开始就有Option Strict On ,这可能不是问题; 你可能自己想出了Action()

暂无
暂无

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

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