Frage

Ich entwickle Boo-Code, der von einem vorhandenen Delegaten in C# benachrichtigt werden muss.Ich erhalte einen Boo-Kompilierungsfehler im Konstruktor von ActionBoo Klasse.Bitte sehen Sie sich die Fehlermeldung zusammen mit jeder Alternative an, die ich ausprobiert habe.

# 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 ist ein vorhandener C#-Code mit einem Multicast-Delegaten.Hier ist eine vereinfachte Version des Originalcodes:

// 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);
    }
}

So habe ich mit Mono (v2.10.9-0) und Boo (v0.9.4.9) unter Linux kompiliert:

$ mcs ActionCS.cs
$ mono booc.exe -r:ActionCS.exe ActionBoo.boo

Der C#-Code und Boos „Alternative Nr. 3“ laufen einwandfrei, wenn Sie Folgendes aufrufen:

$ mono ActionCS.exe
Foo: 5
Bar: 5
$ env MONO_PATH=$MONO_PATH:$BOO_HOME/bin mono ActionBoo.exe
Boo: 3

Weiß jemand, wie man den Boo-Code repariert?

War es hilfreich?

Lösung

In C# += ist nur syntaktischer Zucker für Delegate.Combine, also könnten Sie wahrscheinlich stattdessen Folgendes verwenden:

ActionCS.action = Delegate.Combine(ActionCS.action, Action[of int](Boo))

(Beachten Sie, dass ich nichts über Boo im Speziellen weiß, sondern nur über .NET im Allgemeinen, daher ist dies nur eine fundierte Vermutung.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top