亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

另一個類中的動作委托引用?

另一個類中的動作委托引用?

C#
瀟瀟雨雨 2022-10-23 13:45:21
我正在尋找一種方法來允許另一個類通過調用該類的方法向我的 Action 委托添加方法,而不是在第一個類上調用 Action。這就是我需要的:class Program{    static void Main(string[] args)    {        Action Execute = delegate { };        ProgramTest prog = new ProgramTest(ref Execute);        prog.AddMethod();        Execute();    }}class ProgramTest{    public Action execute;    public ProgramTest(ref Action action)    {        execute = action;    }    public void AddMethod()    {        execute += Print;    }    public void Print()    {        Console.WriteLine("test");        Console.ReadLine();    }}但是,當我調用 Execute() 時,什么也沒有發生。我怎樣才能讓它工作?
查看完整描述

4 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

另一種選擇是將(不可變的)委托放在可變容器中。


public class ActionContainer

{

    public Action Action { get; set; } = () => { };

}


class Program

{

    static void Main(string[] args)

    {

        ActionContainer execute = new ActionContainer();


        ProgramTest prog = new ProgramTest(execute);


        prog.AddMethod();


        execute.Action();

    }

}


class ProgramTest

{

    public ActionContainer execute;


    public ProgramTest(ActionContainer action)

    {

        execute = action;

    }


    public void AddMethod()

    {

        execute.Action += Print;

    }


    public void Print()

    {

        Console.WriteLine("test");

        Console.ReadLine();

    }

}


查看完整回答
反對 回復 2022-10-23
?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

你想要的是這樣的:


class Program

{

    static void Main(string[] args)

    {

        Action Execute = delegate { };


        ProgramTest prog = new ProgramTest(h => Execute += h);


        prog.AddMethod();


        Execute();

    }

}


class ProgramTest

{

    public Action<Action> execute;


    public ProgramTest(Action<Action> action)

    {

        execute = action;

    }


    public void AddMethod()

    {

        execute(Print);

    }


    public void Print()

    {

        Console.WriteLine("test");

        Console.ReadLine();

    }

}

打印test到控制臺。


這是這個模式的一個稍微好一點的版本:


class Program

{

    static void Main(string[] args)

    {

        Action Execute = delegate { };


        ProgramTest prog = new ProgramTest(h => Execute += h, h => Execute -= h);


        var subscription = prog.AddMethod();


        Execute();


        subscription.Dispose();

    }

}


class ProgramTest

{

    public Action<Action> _attach;

    public Action<Action> _detach;


    public ProgramTest(Action<Action> attach, Action<Action> detach)

    {

        _attach = attach;

        _detach = detach;

    }


    public IDisposable AddMethod()

    {

        _attach(Print);

        return Disposable.Create(() => _detach(Print));

    }


    public void Print()

    {

        Console.WriteLine("test");

        Console.ReadLine();

    }

}


public sealed class Disposable : IDisposable

{

    public static IDisposable Create(Action action)

        => new Disposable(action);


    private readonly Action _action;

    private int _disposed;


    private Disposable(Action action)

    {

        _action = action;

    }


    public void Dispose()

    {

        if (Interlocked.Exchange(ref _disposed, 1) == 0)

        {

            _action();

        }

    }

}

我什至會更進一步并定義一個MetaAction- 您可以隨意傳遞它并向其添加方法。


class Program

{

    static void Main(string[] args)

    {

        Action Execute = delegate { };


        MetaAction meta = MetaAction.Create(h => Execute += h, h => Execute -= h);


        var prog = new ProgramTest(meta);


        var subscription = prog.AddMethod();


        Execute();


        subscription.Dispose();

    }

}


public class MetaAction

{

    public static MetaAction Create(Action<Action> attach, Action<Action> detach)

        => new MetaAction(attach, detach);


    public Action<Action> _attach;

    public Action<Action> _detach;


    private MetaAction(Action<Action> attach, Action<Action> detach)

    {

        _attach = attach;

        _detach = detach;

    }


    public IDisposable Subscribe(Action action)

    {

        _attach(action);

        return Disposable.Create(() => _detach(action));

    }

}


public class ProgramTest

{

    public MetaAction _meta;


    public ProgramTest(MetaAction meta)

    {

        _meta = meta;

    }


    public IDisposable AddMethod()

    {

        return _meta.Subscribe(Print);

    }


    public void Print()

    {

        Console.WriteLine("test");

        Console.ReadLine();

    }

}


public sealed class Disposable : IDisposable

{

    public static IDisposable Create(Action action)

        => new Disposable(action);


    private readonly Action _action;

    private int _disposed;


    private Disposable(Action action)

    {

        _action = action;

    }


    public void Dispose()

    {

        if (Interlocked.Exchange(ref _disposed, 1) == 0)

        {

            _action();

        }

    }

}


查看完整回答
反對 回復 2022-10-23
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

您可以通過調用 prog.Execute 而不是 Execute 來使其工作,就像下面的代碼一樣。


class Program

{

    static void Main(string[] args)

    {

        Action Execute = delegate { };

        ProgramTest prog = new ProgramTest(ref Execute);

        prog.AddMethod();

        prog.execute();

    }

}

或者您需要將 Print 方法分配給 main 方法執行委托,如下所示


class Program

{

    static void Main(string[] args)

    {

        Action Execute = delegate { };            

        ProgramTest prog = new ProgramTest(ref Execute);


        Execute += prog.Print;


        prog.AddMethod();


        Execute();


    }

}


查看完整回答
反對 回復 2022-10-23
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

您Program可能會公開一個事件,您的其他類可能會向該事件注冊另一個處理程序:


class Program

{

    public static event Action MyEvent;

    static void Main(string[] args)

    {

        ProgramTest prog = new ProgramTest();


        prog.AddMethod();


        // raise the event and invoke the registered handlers

        MyEvent?.Invoke();

    }

}


class ProgramTest

{

    private Action handler;


    public ProgramTest()

    {

        handler = Print;

    }


    public void AddMethod()

    {

        Program.MyEvent += handler;  // regsiter the execute-delegate to the event

        // or directly: Program.MyEvent += Print;

    }


    public void Print()

    {

        Console.WriteLine("test");

        Console.ReadLine();

    }

}


查看完整回答
反對 回復 2022-10-23
  • 4 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號