C#,将 Action 分配给委托(delegate)

标签 c# delegates type-conversion action

我知道 Action 是一个 Delegate,但我收到以下编译时错误,试图将 Action 分配给委托(delegate)

Cannot implicitly convert type 'System.Action' to 'ADelegate'

public delegate void ADelegate();
Action act = () => Console.WriteLine ("test");
ADelegate del = act;

如何将act赋值给del

最佳答案

C# 不支持委托(delegate)类型之间的强制转换或转换。

尝试像这样创建一个新的 ADelegate:

Action act = () => Console.WriteLine ("test");
ADelegate del = new ADelegate(act);

或者:

Action act = () => Console.WriteLine ("test");
ADelegate del = act.Invoke;

关于C#,将 Action 分配给委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974958/

相关文章:

c# - 在 C# 中基于显式接口(interface)实现实现 == 运算符

c# - C# 中的受约束泛型委托(delegate)

delphi - 从 Delphi 程序调用 C DLL

python - 在 Python pandas 中有选择地将 float 转换为整数和小数

c# - 我应该使用哪些 IVI 引用?

c# - 具有多个条件和组合的 LINQ 方法

Swift 4 委托(delegate)和传递文本字段数据

.net - 如何在 Visual Studio 中自动完成委托(delegate)

error-handling - 怎么样了?运算符与 From 特征交互?

c# - 事件和事件日志之间有什么区别?