最近在做一个项目,涉及到权限方面的问题。 控制权限的思路是 页面通过继承一个父类,权限全部由父类来控制操作。   例如:子页面有个 修改按钮 ,当点击这个修改按钮时 ,先调入执行父类的权限检查方法,通过验证后有此修改权限后
   
才执行修改按钮事件 。否则将不执行。  而子页面的修改事件内 无调用父类方法。
   
    我在父类通过重写 OnPreLoad 方法,将子页面的 修改按钮对象反射获得。
     
          Control con = this.FindControl("Button1");
      Button btn = (con as Button);      EventInfo info = btn.GetType().GetEvent("Click");
      PropertyInfo propertyInfo = (typeof(Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
        EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(btn, null);
        
        FieldInfo fieldInfo = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
        //fieldInfo为空  为什么获取不到?        Delegate d = eventHandlerList[fieldInfo.GetValue(null)];
        
        if (d != null)
        {
            foreach (Delegate temp in d.GetInvocationList())
            {
                btn.Click -= temp as EventHandler;
            }
        }        btn.Click += new EventHandler(btntest_Click);  // 跳转执行权限检查方法        base.OnPreLoad(e);    
    
     还有什么其他方法 让按钮事件先执行我定义的事件在后执行他本身事件?  坐等完美解答。

解决方案 »

  1.   

    抽象类 和 代理 都能满足。abstract class AbstractParentController
    {
        public abstract bool  HasPermission();
        protected abstract void ValidatedOperation();
        public virtual void Excute()
        {
               if(HasPermission())
               {
                  ValidatedOperation();
               }
               else
               {
                  throw new Exception("No permission.");
                }
        }
        
    }
      

  2.   

    代理就是interface IController
    {
        void Excute();
    }interface IPermissionChecker
    {
        bool HasPermission(); 
    }class Controller : IControllerclass  ControllerProxy :IController,IPermissionChecker
    {
         
         private bool HasPermission(){}
        public void Excute()
       { 
           if(HasPermission())
           {
               Excute();
           }
        }
    }这个距离你的需求有点远
      

  3.   

       先谢谢 sunzongbao2007  帮我回复, 虽然没太看懂。  我已经找到比较简单的方法了。    Control con = this.FindControl("Button1");
          Button btn = (con as Button);      EventInfo info = btn.GetType().GetEvent("Click");
          PropertyInfo propertyInfo = (typeof(Button)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
            EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(btn, null);     直接将 判断是否有权限 然后 将 eventHandlerList.Dispose();
         这样子类事件将无法执行了。 问题解决了。