Question

Je développe du code Boo qui doit être notifié par un délégué existant en C#.Je reçois une erreur de compilation Boo dans le constructeur du ActionBoo classe.Veuillez consulter le message d'erreur ainsi que toutes les alternatives que j'ai essayées.

# 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 est un code C# existant avec un délégué de multidiffusion.Voici une version simplifiée du code original :

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

Voici comment j'ai compilé avec Mono (v2.10.9-0) et Boo (v0.9.4.9) sous Linux :

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

Le code C# et l'"Alternative #3" de Boo fonctionnent correctement en appelant :

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

Est-ce que quelqu'un sait comment corriger le code Boo ?

Était-ce utile?

La solution

En C# += est juste du sucre syntaxique pour Delegate.Combine, vous pourriez donc probablement l'utiliser à la place :

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

(notez que je ne sais rien de Boo en particulier, seulement de .NET en général, donc ce n'est qu'une supposition éclairée)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top