Warloc22
Warloc22

Reputation: 3

Parameter action<T> to an instance

I want to extract an instance from my parameter inside my car class and in my code I have something like:

class Car
{
    static Car MakeCustomCarWithColor(string color)
    {
        return new Car(c => c.Color = color);
    }
    public Car(Action<ICustomizeCar> customization)
    {
        //here I want to access the values from customization
        // how to convert Action<ICustomizeCar>
        ICustomizeCar myCustom = ?????????????????????????????;
        // It prints the color passed from Builder class            
        Console.WriteLine(myCustom.Color);
    }

    public interface ICustomizeCar
    {
        string Color { get; set; }
    }
}

Upvotes: 0

Views: 62

Answers (1)

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5843

In my mind your Car class should implements ICustomizeCar interface, so you can send this pointer to the action.

class Car
  : ICustomizeCar
{
   ICustomizeCar::Color {get;set;} 

    static Car MakeCustomCarWithColor(string color)
    {
        return new Car(c => c.Color = color);
    }
    public Car(Action<ICustomizeCar> customization)
    {
        customization(this);
        Console.WriteLine(myCustom.Color);
    }

    public interface ICustomizeCar
    {
        string Color { get; set; }
    }
}

Upvotes: 1

Related Questions