简体   繁体   English

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

[英]Converting VB.NET code to C#: Cannot convert lambda expression to type 'Delegate' because it is not a delegate type

Okay, so I've got I got a VB project that I'm converting to C#. 好的,所以我有一个要转换为C#的VB项目。 So far, so good. 到现在为止还挺好。 The problem is delegates/actions are quite different between the two languages and I'm struggling to work out the difference. 问题是两种语言之间的委托/动作完全不同,我正在努力找出差异。

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

And in C# 在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));
}

The last line above is causing the CS1660 Cannot convert lambda expression to type 'Delegate' because it is not a delegate type error. 上面的最后一行导致CS1660 Cannot convert lambda expression to type 'Delegate' because it is not a delegate type错误。

Your register method should be defined as: 您的注册方法应定义为:

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

Or you need to explicitly wrap your lambda in an Action : 或者您需要将lambda显式地包装在Action

private void Setup()
{
    Register(5, new Action<string>((string a) => LogName(a)));
}

暂无
暂无

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

相关问题 C#无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型 - C# Cannot convert lambda expression to type 'Delegate' because it is not a delegate type 无法将Lambda表达式转换为类型,因为它不是委托 - Cannot convert lambda expression to type because it is not a delegate 无法将Lambda表达式转换为类型“ Delegate”,因为它不是委托类型MVC 5 - Cannot convert lambda expression to type 'Delegate' because it is not a delegate type MVC 5 无法将lambda表达式转换为“委托”类型,因为它不是委托类型 - Cannot convert lambda expression to type 'Delegate' because it is not a delegate type 无法将lambda表达式转换为委托类型,因为委托不是类型 - Cannot convert lambda expression to type delegate because delegate is not a type “无法将lambda表达式转换为&#39;string&#39;类型,因为它不是委托类型”在C#中查询数据集 - “Cannot convert lambda expression to type 'string' because it is not a delegate type” querying dataset in C# C# 无法将 lambda 表达式转换为“动态”类型,因为它不是委托类型 - C# Cannot convert lambda expression to type 'dynamic' because it is not a delegate type C# Linq 与 MVC,无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型 - C# Linq with MVC, Cannot convert lambda expression to type 'string' because it is not a delegate type C#显示ExpandoObject的数据-无法将lambda表达式转换为类型“…”,因为它不是委托 - C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate 无法将lambda表达式转换为类型&#39;string&#39;,因为它不是具有NotifyOfPropertyChange的委托类型 - Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM