static void Main(string[] args) { UnityContainer contanier = new UnityContainer(); //向IOC容器中注册浙江系菜和四川系菜 contanier.RegisterType <IMess, SiChuanFood>(); contanier.RegisterType <IMess, ZhengJiangFood>(); //IOC容器会智能的选择和调用合适的构造函数,以创建依赖的对象,如果被选择的构造函数具有相应的参数,IOC容器在调用构造函数之前会解析注册的依赖关系并自行获得相应的参数。 //获取可行的参数后,将参数注入到对应的类中 IEmployee employee = contanier.Resolve <SiChuanEmployee>(); IEmployee employees = contanier.Resolve <ZheJiangEmployee>(); employee.EatFood(); employees.EatFood(); Console.ReadLine(); }