using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
namespace IOCDemo1
{
class Program
{
static void Main(string[] args)
{
ContainerCodeTest1();
}
public static void ContainerCodeTest1()
{
IUnityContainer container = new UnityContainer();
//默认注册(无命名),如果后面还有默认注册会覆盖前面的
container.RegisterType<IClass, CbClass>();
//命名注册
container.RegisterType<IClass, EcClass>("ec");
//命名注册
container.RegisterType<IClass, EeClass>("Ee");
//解析默认对象
IClass cbClass = container.Resolve<IClass>();
Console.WriteLine(cbClass.ShowInfo("4"));
//指定命名解析对象
IClass ecClass = container.Resolve<IClass>("ec");
ecClass.ShowInfo("4");
//指定命名解析对象
IClass eeClass = container.Resolve<IClass>("Ee");
eeClass.ShowInfo("4");
获取容器中所有IClass的注册的已命名对象
//IEnumerable<IClass> classList = container.ResolveAll<IClass>();
//foreach (var item in classList)
//{
// item.ShowInfo("4");
//}
}
}
/// <summary>
/// 班级接口
/// </summary>
public interface IClass
{
string ClassName { get; set; }
string ShowInfo(string s);
}
/// <summary>
/// 计科班
/// </summary>
public class CbClass : IClass
{
public string ClassName { get; set; }
public string ShowInfo(string s)
{
Console.WriteLine("计科班:{0}", ClassName);
Console.ReadLine();
return s;
}
}
/// <summary>
/// 电商班
/// </summary>
public class EcClass : IClass
{
public string ClassName { get; set; }
public string ShowInfo(string s)
{
Console.WriteLine("电商班:{0}", ClassName);
Console.ReadLine();
return s;
}
}
public class EeClass : IClass
{
public string ClassName { get; set; }
public string ShowInfo(string s)
{
Console.WriteLine("电商版:{0}",ClassName);
Console.ReadLine();
return s;
}
}
}