Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将VB.NET代码转换为C:无法将lambda表达式转换为“Delegate”类型,因为它不是委托类型_C#_Vb.net_Delegates - Fatal编程技术网

C# 将VB.NET代码转换为C:无法将lambda表达式转换为“Delegate”类型,因为它不是委托类型

C# 将VB.NET代码转换为C:无法将lambda表达式转换为“Delegate”类型,因为它不是委托类型,c#,vb.net,delegates,C#,Vb.net,Delegates,好的,我有一个VB项目,我正在转换成C。到目前为止,很好。问题是这两种语言之间的委托/操作非常不同,我正在努力找出差异 Private methods As New Dictionary(Of Integer, [Delegate]) Private Sub Register(id As Integer, method As [Delegate]) methods.Add(id, method) End Sub Private Sub LogName(name As String)

好的,我有一个VB项目,我正在转换成C。到目前为止,很好。问题是这两种语言之间的委托/操作非常不同,我正在努力找出差异

Private methods As New Dictionary(Of Integer, [Delegate])

Private Sub Register(id As Integer, method As [Delegate])
    methods.Add(id, method)
End Sub

Private Sub LogName(name As String)
    Debug.Print(name)
End Sub

Private Sub Setup()
    Register(Sub(a As String) LogName(a))
End Sub
在C中

private Dictionary<int, Delegate> methods;

private void Register(int id, Delegate method)
{
    methods.Add(id, method);
}

private void LogName(string name)
{
    Debug.Print(name);
}

private void Setup()
{
    Register((string a) => LogName(a));
}

上面的最后一行导致CS1660无法将lambda表达式转换为“Delegate”类型,因为它不是委托类型错误。

您的注册方法应定义为:

private void Register(int id, Action<string> method)
{
    methods.Add(id, method);
}

您是否尝试注册新Actionstring a=>LogNamea;或Registernew Actionstring a=>{LogNamea;};是的,不行。相同错误的变体。感谢Rob,第二部分是解决方案。强制转换也可以工作:Register0,Actionstring A=>LogNamea;
private void Setup()
{
    Register(5, new Action<string>((string a) => LogName(a)));
}