testing - 1 year ago 67
C# Question

This is from the factory pattern, where a property is used to get an instance via

Create
:

public class Dialer
{
public static Func<Dialer> Create;
public bool MakeCall(string number) ...
public Dialer(IDialer impl) { ... }
}


Then a lambda expression is assigned to the property delegate in the platform-specific project with

Dialer.Create = () => new Dialer(new PhoneDialeriOS());


and to get an instance in the platform-independent project I use

this.dialer = Dialer.Create();


Now I'm looking to use

public static Action<Dialer> Create;


If I get this right, the assignment now is

Dialer.Create = (d) => new Dialer(new PhoneDialeriOS());


but how do I get an instance?

this.dialer = // ?


By using
this.dialer = Dialer.Create();
I get

Error CS7036 There is no argument given that corresponds to the required formal parameter 'obj' of 'Action'

But it doesn't make sense to pass an instance of
PhoneDialeriOS
here, because there is no access to it in the platform-independent code. I think the example I'm regarding to is misleading or I'm missing something.

Action<Dialer> is a a delegate that receives a Dialer instance, and returns void. It's an Action, after all. If you want it to return a value (and get an argument), you need to use Func<Dialer, Dialer> instead.