c# 为什么 delegate\event 在传递给另一个 class 后没有得到更新

c# Why delegate\event doesn't get updated after passing it to another class

我在调用将事件传递给特定 class 后添加的委托时遇到问题,我认为委托将更新作为对象获取..

例如,我将委托传递给 class:

更新代码(由于评论中的一些问题)

这实际上是 +- 我需要如何 运行 它,其中 "ExecutorOnCalculationCompleted" 永远不会调用(请忽略睡眠和同步,我将我的代码缩减到需要的部分)

    class Executor
{

    public delegate void CalculationCompletEventHandler(int score);

    public event CalculationCompletEventHandler CalculationCompleted;

    public void Start()
    {
        CalculationCompleted += OnCalculationCompleted;
        Plus plus = new Plus(1, 2, CalculationCompleted);
        Minus minus =  new Minus(5, 2, CalculationCompleted);
        Multi multi =  new Multi(5, 2, CalculationCompleted);
        ...
        ...    They will work async...
    }

    private void OnCalculationCompleted(int score)
    {
        Console.WriteLine("OnCalculationCompleted , score=" + score);
    }
}

class Plus
{
    private Executor.CalculationCompletEventHandler _calculationCompletEventHandler;
    private int a;
    private int b;

    public Plus(int a, int b,Executor.CalculationCompletEventHandler calculationCompletEventHandler)
    {
        this.a = a;
        this.b = b;
        _calculationCompletEventHandler = calculationCompletEventHandler;
    }

    public void Calculate()
    {
        _calculationCompletEventHandler?.Invoke(a+b);
    }
}



class Program
{

    static void Main(string[] args)
    {
        Executor executor = new Executor();
        executor.Start(); // async action
        executor.CalculationCompleted += ExecutorOnCalculationCompleted;
        ...
    }

    // This method doesn't get invoked when the class inside Executor fire the event.
    private static void ExecutorOnCalculationCompleted(int score)
    {
        Console.WriteLine("ExecutorOnCalculationCompleted , score=" + score);
    }
}

您正在构造函数中直接传递委托。我认为您想将事件放入 class。示例:

class Plus
{
    public delegate void CalculationCompletEventHandler(int score);
    public event CalculationCompletEventHandler CalculationCompleted;

    private int a;
    private int b;

    public Plus(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    public void Calculate()
    {
        if (CalculationCompleted != null)
        {
            CalculationCompleted(a + b);
        }
    }
}

您现在可以像这样使用它了:

void Main()
{
    Plus plus = new Plus(1, 2);
    plus.CalculationCompleted += OnCalculationCompleted;
    plus.Calculate();

    plus.CalculationCompleted += OnCalculationCompleted2;
    plus.Calculate();        
}

private void OnCalculationCompleted(int score)
{
    Console.WriteLine("OnCalculationCompleted , score=" + score);
}

private void OnCalculationCompleted2(int score)
{
    Console.WriteLine("OnCalculationCompleted2 , score=" + score);
}

如评论中所述,如果委托类型用于程序的不同部分,您可能只需要事件本身。

问题更新后

如果您想要执行程序中的事件,我只需将一个操作传递给您的每个 classes,例如 PlusMultiply,...并调用该事件来自执行者:

class Executor
{
    public delegate void CalculationCompletEventHandler(int score);
    public event CalculationCompletEventHandler CalculationCompleted;

    public void Start()
    {
        CalculationCompleted += OnCalculationCompleted;
        Plus plus = new Plus(1, 2, FireEvent);        
    }

    private void FireEvent(int score)
    {
        if (CalculationCompleted != null)
        {
            CalculationCompleted(score);
        }
    }

    private void OnCalculationCompleted(int score)
    {
        Console.WriteLine("OnCalculationCompleted , score=" + score);
    }
}

class Plus
{
    private int a;
    private int b;
    private Action<int> completionAction

    public Plus(int a, int b, Action<int> completionAction)
    {
        this.a = a;
        this.b = b;
        this.completionAction = completionAction;       
    }

    public void Calculate()
    {
        this.completionAction(a + b);
    }
}