Factory Pattern but with object Parameters

前端 未结 7 1889
春和景丽
春和景丽 2021-02-02 11:05

Take the following classic factory pattern:

public interface IPizza
{
    decimal Price { get; }
}

public class HamAndMushroomPizza : IPizza
{
    decimal IP         


        
7条回答
  •  深忆病人
    2021-02-02 11:52

    You can use reflection:

    using System.Reflection;
    
    // ...
    
    public override IPizza CreatePizza(PizzaType pizzaType, params object[] parameters) {
                return (IPizza)
                       Activator.CreateInstance(
                            Assembly
                                 .GetExecutingAssembly()
                                 .GetType(pizzaType.ToString()),
                            parameters);
            }
    

提交回复
热议问题