7
votes

I have a class that has the following property:

public Action<bool> Action { get; private set; }

And I have a constructor that takes Action<bool> as an argument.

Now I want to add another constructor that accepts an object of type Action. How can I convert Action to Action<bool>? The bool-parameter should be true in this case.

2

2 Answers

13
votes
public class Foo
{
    public Foo(Action<bool> action)
    {
        // Some existing constructor
    }

    public Foo(Action action): this(x => action())
    {
        // Your custom constructor taking an Action and 
        // calling the existing constructor
    }
}

Now you could instantiate this class in 2 ways depending on which one of the 2 constructors you want to invoke:

  1. var foo = new Foo(x => { Console.WriteLine("Hello"); }); (calls the first ctor)
  2. var foo = new Foo(() => { Console.WriteLine("Hello"); }); (calls the second ctor)
6
votes
Action a = () => aboolaction(true);