[英]Error when adding a Boo method to a C# multicast delegate
I'm developing some Boo code that needs to be notified by an existing delegate in C#. 我正在开发一些Boo代码,需要由C#中的现有委托来通知。 I'm getting a Boo compilation error in the constructor of the
ActionBoo
class. 我在
ActionBoo
类的构造函数中遇到Boo编译错误。 Please see the error message along with every alternative I tried. 请查看错误消息以及我尝试过的所有替代方法。
# Boo
import System
class ActionBoo:
def constructor():
# Alternative #1
# ActionBoo.boo(9,25): BCE0051: Operator '+' cannot be used with a left hand side of type 'System.Action' and a right hand side of type 'callable(int) as void'.
ActionCS.action += Boo
# Alternative #2
# ActionBoo.boo(13,25): BCE0051: Operator '+' cannot be used with a left hand side of type 'System.Action' and a right hand side of type 'System.Action'.
ActionCS.action += Action[of int](Boo)
# Alternative #3
# This works, but it resets the delegate already set up in C#
ActionCS.action = Boo
def Boo(boo as int):
print 'Boo: ' + boo
actioncs = ActionCS()
actionBoo = ActionBoo()
ActionCS.action(3)
ActionCS
is an existing C# code with a multicast delegate. ActionCS
是具有多播委托的现有C#代码。 Here is a simplified version of the original code: 这是原始代码的简化版本:
// C#
using System;
public class ActionCS
{
public static Action<int> action;
public ActionCS()
{
action += Foo;
action += Bar;
}
public void Foo(int foo)
{
Console.WriteLine("Foo: " + foo);
}
public void Bar(int bar)
{
Console.WriteLine("Bar: " + bar);
}
public static void Main(string[] args)
{
ActionCS actioncs = new ActionCS();
action(5);
}
}
Here is how I compiled with Mono (v2.10.9-0) and Boo (v0.9.4.9) on Linux: 这是我在Linux上使用Mono(v2.10.9-0)和Boo(v0.9.4.9)进行编译的方式:
$ mcs ActionCS.cs
$ mono booc.exe -r:ActionCS.exe ActionBoo.boo
The C# code and Boo's "Alternative #3" run fine by calling: 通过调用以下命令,C#代码和Boo的“ Alternative#3”可以正常运行:
$ mono ActionCS.exe
Foo: 5
Bar: 5
$ env MONO_PATH=$MONO_PATH:$BOO_HOME/bin mono ActionBoo.exe
Boo: 3
Does anyone know how to fix the Boo code? 有人知道如何修复Boo代码吗?
In C# +=
is just syntactic sugar for Delegate.Combine
, so you could probably use that instead: 在C#中,
+=
只是Delegate.Combine
语法糖,因此您可以改用它:
ActionCS.action = Delegate.Combine(ActionCS.action, Action[of int](Boo))
(note that I don't know anything about Boo specifically, only about .NET in general, so this is just an educated guess) (请注意,我对Boo并没有任何了解,仅对.NET有所了解,所以这只是有根据的猜测)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.