1public class PageBase : System.Web.UI.Page
 2    {
 3        protected BindingFlags BINDING_FLAGS_SET
 4                    = BindingFlags.Public
 5                    | BindingFlags.SetProperty
 6                    | BindingFlags.Instance
 7                    | BindingFlags.SetField
 8                    ;
 9
10        protected override void OnInit(EventArgs e)
11        {
12            IWindsorContainer container = ObtainContainer();
13
14            Type type = this.GetType();
15
16            PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
17
18            foreach (PropertyInfo propertie in properties)
19            {
20                string pname = propertie.Name;
21
22                if (container.Kernel.HasComponent(pname))
23                {
24                    propertie.SetValue(this, container[pname], null);
25                }

26            }

27
28            base.OnInit(e);
29        }

30
31
32        public IWindsorContainer ObtainContainer()
33        {
34
35            IContainerAccessor containerAccessor =
36
37            HttpContext.Current.ApplicationInstance as IContainerAccessor;
38            if (containerAccessor == null)
39            {
40                throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
41            }

42
43            IWindsorContainer container = containerAccessor.Container;
44            if (container == null)
45            {
46                throw new ApplicationException("HttpApplication 得不到容器的实例");
47            }

48            return container;
49        }

50    }
posted on 2007-09-25 13:35  选择流浪  阅读(345)  评论(1编辑  收藏  举报