先添加一个接口
namespace IContract { public interface IBase { void ShowMessage(); } }
再添加两个实现类
namespace Contract { public class AContract : IBase { public void ShowMessage() { MessageBox.Show("A"); } } }
namespace Contract { public class BContract : IBase { public void ShowMessage() { MessageBox.Show("B"); } } }
namespace TinyIOCTest { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { TinyIoCContainer _container = TinyIoCContainer.Current; public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { //_container.Register<IBase, AContract>(); _container.Register<AContract>(); _container.AutoRegister(); } private void Button_Click_1(object sender, RoutedEventArgs e) { _container.Register<IBase, BContract>("BaseType"); } private void Button_Click_2(object sender, RoutedEventArgs e) { _container.Resolve<IBase>().ShowMessage(); _container.Resolve<IBase>("BaseType").ShowMessage(); } } }