حدث خطأ عند إضافة أسلوب Boo إلى مفوض البث المتعدد C#

StackOverflow https://stackoverflow.com//questions/20047304

  •  26-12-2019
  •  | 
  •  

سؤال

أقوم بتطوير بعض أكواد Boo التي يجب أن يتم إعلامها بواسطة مندوب حالي في C#.أتلقى خطأ في تجميع Boo في مُنشئ ملف ActionBoo فصل.الرجاء الاطلاع على رسالة الخطأ مع كل بديل قمت بتجربته.

# 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 هو رمز C# موجود مع مندوب البث المتعدد.فيما يلي نسخة مبسطة من الكود الأصلي:

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

إليك كيفية التجميع باستخدام Mono (v2.10.9-0) وBoo (v0.9.4.9) على Linux:

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

يعمل رمز C# و"البديل رقم 3" الخاص بـ Boo بشكل جيد عن طريق الاتصال:

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

هل يعرف أحد كيفية إصلاح رمز Boo؟

هل كانت مفيدة؟

المحلول

شركة# += هو مجرد سكر نحوي ل Delegate.Combine, ، لذا ربما يمكنك استخدام ذلك بدلاً من ذلك:

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

(لاحظ أنني لا أعرف أي شيء عن Boo على وجه التحديد، فقط عن .NET بشكل عام، لذلك هذا مجرد تخمين مدروس)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top