unity 三种注入示例
2018-06-23 22:46:31来源:未知 阅读 ()
1 /* 2 * 演示Unity 注入 3 * */ 4 using Microsoft.Practices.Unity; 5 using System; 6 7 namespace Unity.Property.Inject 8 { 9 class Program 10 { 11 public static IUnityContainer container; 12 13 static void Main(string[] args) 14 { 15 container = new UnityContainer(); 16 17 //PropertyInject(); 18 //ConstructorInject(); 19 MethodInject(); 20 21 Console.ReadLine(); 22 } 23 24 /// <summary> 25 /// 构造注入 26 /// </summary> 27 static void ConstructorInject() 28 { 29 Console.WriteLine("构造注入"); 30 container.RegisterType<ILog, ALog>(); 31 //container.RegisterType<ILog, BLog>(); 32 33 container.RegisterType<IPeople, Man>(); 34 //container.RegisterType<IPeople, Woman>(); 35 36 var people = container.Resolve<IPeople>(); 37 people.Print2(); 38 } 39 40 /// <summary> 41 /// 属性注入 42 /// </summary> 43 static void PropertyInject() 44 { 45 Console.WriteLine("属性注入"); 46 //通过Register不同的ILog的派生类型,达到用不同的日志组件进行打印 47 48 //container.RegisterType<ILog, ALog>(); 49 container.RegisterType<ILog, BLog>(); 50 51 container.RegisterType<IPeople, Man>(); 52 //container.RegisterType<IPeople, Woman>(); 53 54 var people = container.Resolve<IPeople>(); 55 people.Print(); 56 } 57 58 static void MethodInject() 59 { 60 Console.WriteLine("函数注入"); 61 //通过Register不同的ILog的派生类型,达到用不同的日志组件进行打印 62 63 //container.RegisterType<ILog, ALog>(); 64 container.RegisterType<ILog, BLog>(); 65 66 container.RegisterType<IPeople, Man>(); 67 //container.RegisterType<IPeople, Woman>(); 68 69 var people = container.Resolve<IPeople>(); 70 people.Logger.Write("函数注入 " + people.Logger != null ? "成功" : "失败"); 71 } 72 } 73 74 public interface IPeople 75 { 76 /// <summary> 77 /// 属性注入用 78 /// </summary> 79 ILog Logger { get; set; } 80 81 /// <summary> 82 /// 构造注入用 83 /// </summary> 84 ILog Logger2 { get; set; } 85 86 String Name { get; set; } 87 88 String Sex { get; set; } 89 90 /// <summary> 91 /// 属性注入用 92 /// </summary> 93 void Print(); 94 95 /// <summary> 96 /// 构造注入用 97 /// </summary> 98 void Print2(); 99 100 /// <summary> 101 /// 函数注入用 102 /// </summary> 103 void Print3(ILog logger); 104 } 105 106 public class Man : IPeople 107 { 108 [Dependency] 109 public ILog Logger { get; set; } 110 public ILog Logger2 { get; set; } 111 112 public string Name { get; set; } 113 114 public string Sex { get; set; } 115 116 public Man(ILog logger2) { this.Logger2 = logger2; } 117 118 public void Print() 119 { 120 Logger.Write(base.ToString()); 121 } 122 123 public void Print2() 124 { 125 Logger.Write(base.ToString()); 126 } 127 128 [InjectionMethod] 129 public void Print3(ILog logger) 130 { 131 this.Logger = logger; 132 } 133 } 134 135 public class Woman : IPeople 136 { 137 138 [Dependency] 139 public ILog Logger { get; set; } 140 public ILog Logger2 { get; set; } 141 142 public string Name { get; set; } 143 144 public string Sex { get; set; } 145 146 public Woman(ILog logger2) { this.Logger2 = logger2; } 147 148 public void Print() 149 { 150 Logger.Write(base.ToString()); 151 } 152 153 public void Print2() 154 { 155 Logger.Write(base.ToString()); 156 } 157 158 [InjectionMethod] 159 public void Print3(ILog logger) 160 { 161 this.Logger = logger; 162 } 163 } 164 165 166 public interface ILog 167 { 168 void Write(String msg); 169 } 170 171 public class ALog : ILog 172 { 173 public void Write(String msg) { Console.WriteLine("[A] {0}", msg); } 174 } 175 176 public class BLog : ILog 177 { 178 public void Write(String msg) { Console.WriteLine("[B] {0}", msg); } 179 } 180 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:test1
- css排版常用样式、三种不同html类型、css布局盒模型介绍 2020-03-06
- css的三种导入方式 2020-01-11
- CSS的三种样式,有一种你肯定不知道 2019-12-01
- HTML连载49-清除浮动的第三种方式(内外墙法) 2019-11-14
- 前端之css(一) 2019-11-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash