- nacu
- 等级: 初级会员
- 性别:
- 文章: 10
- 积分: 70
- 来自: 上海
|
虽然c# Meta programing 的能力不够
不过新特性还是比较爽的
用这些写了个IoC类
挺有意思
c# 代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using NUnit.Framework;
-
- namespace Test
- {
-
-
-
- public interface IComponentService<t> </t>
- {
- T GetComponent();
- }
-
-
-
-
- public class SingleComponentService<t>:IComponentService<t> </t></t>
- {
- private Func
- private MicroContainer container;
-
- public SingleComponentService(MicroContainer container,Func
- {
- this.container = container;
- this.registerFunction = registerFunction;
- }
-
- #region IComponentService Members
-
- public T GetComponent()
- {
- return (T)this.registerFunction(this.container);
- }
-
- #endregion
- }
-
-
-
-
- public class CachedComponentService<t>:IComponentService<t> </t></t>
- {
- private IComponentService<t> realService; </t>
-
- private T result = default(T);
-
- public CachedComponentService(IComponentService<t> realService) </t>
- {
- this.realService = realService;
- }
-
- #region IComponentService Members
-
- public T GetComponent()
- {
- if (this.result == null)
- this.result = this.realService.GetComponent();
- return this.result;
- }
-
- #endregion
- }
-
- public class ServiceNotExistException : Exception
- {
- public ServiceNotExistException(string message):base(message)
- {
- }
- }
-
-
-
-
-
-
-
-
-
-
- public class MicroContainer
- {
- private IDictionary<string, object> componets
- = new Dictionary<string, object>();
-
- private MicroContainer parentContainer;
-
-
- public MicroContainer():this(null)
- {
- }
-
- public MicroContainer(MicroContainer parentContainer)
- {
- this.parentContainer = parentContainer;
- }
-
-
-
-
-
-
-
- public void Register<t>(</t>string key, Func
- {
- this.componets.Add(key, new SingleComponentService<t>(</t>this,registerFunction));
- }
-
-
-
-
-
-
-
- public void RegisterSingleton<t>(</t>string key, Func
- {
- this.componets.Add(key, new CachedComponentService<t>(</t>new SingleComponentService<t>(</t>this,registerFunction)));
- }
-
-
-
-
-
-
-
- public void RegisterService<t>(</t>string key,IComponentService<t> service) </t>
- {
- this.componets.Add(key, service);
- }
-
-
-
-
-
-
-
- public T GetComponent<t>(</t>string key)
- {
- if (this.componets.ContainsKey(key))
- return ((IComponentService<t>)</t>this.componets[key]).GetComponent();
-
-
- if (this.parentContainer != null)
- return this.parentContainer.GetComponent<t>(key); </t>
- else
- throw new ServiceNotExistException("component not exist:" + key);
- }
- }
- }
使用方法如下
测试类:
c# 代码
- public class MainClass
- {
- public string Name { get; set; }
-
- public int DoTimes { get; set; }
-
- public FirstInterface Sth { get; set; }
-
- public void Run()
- {
- for (int i = 0; i < this.DoTimes; i++)
- {
- this.Sth.Run(this.Name);
- }
- }
-
- public void RunForMetaCall()
- {
- Console.WriteLine("meta method call");
- }
-
- public string MethodMissing(string name)
- {
- return "hello" + name;
- }
- }
-
- public interface FirstInterface
- {
- void Run(string name);
- }
-
- public class FirstClass : FirstInterface
- {
- #region FirstInterface Members
-
- public void Run(string name)
- {
- Console.WriteLine("first hello {0}", name);
- }
-
- #endregion
- }
-
- public class SecondClass : FirstInterface
- {
- #region FirstInterface Members
-
- public void Run(string name)
- {
- Console.WriteLine("second hello {0}", name);
- }
-
- #endregion
- }
c# 代码
- var parentContainer = new MicroContainer();
-
-
- var container = new MicroContainer(parentContainer);
-
-
-
- container.Register<firstinterface>("FirstClass", c => new FirstClass()); </firstinterface>
-
- container.Register("Name", c => "xiao li");
- container.Register("Name2", c => "xiao zhang");
-
- container.Register("Times", c => 3);
- container.Register("Times2", c => 1);
-
- container.Register("MainClass",
- c => new MainClass
- {
- Name = c.GetComponent<string>("Name2"),
- DoTimes = c.GetComponent<int>("Times2"),
- Sth = c.GetComponent<firstinterface>("SecondClass") </firstinterface>
- });
-
- parentContainer.Register<firstinterface>("SecondClass", </firstinterface>
- c => new SecondClass());
-
-
-
- var mainClass = container.GetComponent<mainclass>("MainClass"); </mainclass>
- mainClass.Run();
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|