求WebForm版构造函数注入例子~新年快乐~
求WebForm版构造函数注入例子~新年快乐~
求WebForm版构造函数注入例子~新年快乐~如题~

解决方案 »

  1.   


    嗯 必须提供没有参数的构造函数
    private T _interface
    public ctor()
    {
        _interface=Ioc.Get<T>
    }我现在是这样实现的,不知道好不好~
      

  2.   

    google emit,不过meta programming不适合C#这样的语言。就好比oop不适合VB一样。
      

  3.   

    是不是为了IoC?
    下面的例子是用 HttpModule/CustomAttribute/Windsor 实现的注入
       // index.aspx.cs
        public partial class IndexPage : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Logger.Write("page loading");
            }        [Inject]
            public ILogger Logger { get; set; }
        }    // WindsorHttpModule.cs
        public class WindsorHttpModule : IHttpModule
        {
            private HttpApplication _application;
            private IoCProvider _iocProvider;        public void Init(HttpApplication context)
            {
                _application = context;
                _iocProvider = context as IoCProvider;            if(_iocProvider == null)
                {
                    throw new InvalidOperationException("Application must implement IoCProvider");
                }            _application.PreRequestHandlerExecute += InitiateWindsor;
            }        private void InitiateWindsor(object sender, System.EventArgs e)
            {
                Page currentPage = _application.Context.CurrentHandler as Page;
                if(currentPage != null)
                {
                    InjectPropertiesOn(currentPage);
                    currentPage.InitComplete += delegate { InjectUserControls(currentPage); };
                }
            }        private void InjectUserControls(Control parent)
            {
                if(parent.Controls != null)
                {
                    foreach (Control control in parent.Controls)
                    {
                        if(control is UserControl)
                        {
                            InjectPropertiesOn(control);
                        }
                        InjectUserControls(control);
                    }
                }
            }        private void InjectPropertiesOn(object currentPage)
            {
                PropertyInfo[] properties = currentPage.GetType().GetProperties();
                foreach(PropertyInfo property in properties)
                {
                    object[] attributes = property.GetCustomAttributes(typeof (InjectAttribute), false);
                    if(attributes != null && attributes.Length > 0)
                    {
                        object valueToInject = _iocProvider.Container.Resolve(property.PropertyType);
                        property.SetValue(currentPage, valueToInject, null);
                    }
                }
            }
        }    // Global.asax.cs
        public class Global : System.Web.HttpApplication, IoCProvider
        {
            private IWindsorContainer _container;        public override void Init()
            {
                base.Init();            InitializeIoC();
            }        private void InitializeIoC()
            {
                _container = new WindsorContainer();
                _container.AddComponent<ILogger, Logger>();
            }        public IWindsorContainer Container
            {
                get { return _container; }
            }
        }    public interface IoCProvider
        {
            IWindsorContainer Container { get; }
        }