То, что вы хотите, это:
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();
}
}
}