<script type="text/javascript">google_ad_client = "pub-2048279401139630";google_ad_slot = "8856771542";google_ad_width = 728;google_ad_height = 90;document.write("<s"+"cript type='text/javascript' s"+"rc='http://pagead2.googlesyndication.com/pagead/show_ads"+"."+"js'></scr"+"ipt>");</script>虽然c# Meta programing 的能力不够
不过新特性还是比较爽的
用这些写了个IoC类
挺有意思
使用方法如下
测试类:
不过新特性还是比较爽的
用这些写了个IoC类
挺有意思
c# 代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using NUnit.Framework;
- namespace Test
- {
- ///
- /// service interface
- ///
- public interface IComponentService
- {
- T GetComponent();
- }
- ///
- /// normal service
- ///
- public class SingleComponentService:IComponentService
- {
- 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
- }
- ///
- /// singleton service
- ///
- public class CachedComponentService:IComponentService
- {
- private IComponentService realService;
- private T result = default(T);
- public CachedComponentService(IComponentService realService)
- {
- 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)
- {
- }
- }
- ///
- /// 小型Micro 容器
- /// todo: 1 父容器 -ok
- /// 2 service regist -ok
- /// 3 generic -ok
- /// 4 singlton,lazy initial -ok
- /// 5 aop,attribute driver ,define to kernelSupport
- ///
- 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(string key, Func
- {
- this.componets.Add(key, new SingleComponentService(this,registerFunction));
- }
- ///
- /// 注册singleton服务
- ///
- ///
- ///
- ///
- public void RegisterSingleton(string key, Func
- {
- this.componets.Add(key, new CachedComponentService(new SingleComponentService(this,registerFunction)));
- }
- ///
- /// 注册服务
- ///
- ///
- ///
- ///
- public void RegisterService(string key,IComponentService service)
- {
- this.componets.Add(key, service);
- }
- ///
- /// 取得组件
- ///
- ///
- ///
- /// 组件
- public T GetComponent(string key)
- {
- if (this.componets.ContainsKey(key))
- return ((IComponentService)this.componets[key]).GetComponent();
- if (this.parentContainer != null)
- return this.parentContainer.GetComponent(key);
- 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("FirstClass", c => new FirstClass());
- 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("SecondClass")
- });
- parentContainer.Register("SecondClass",
- c => new SecondClass());
- var mainClass = container.GetComponent("MainClass");
- mainClass.Run();