如我有类MyClass他里面的每一个方法都要执行Validation.Check方法进行数据验证;
我现在写的方法是在每一个事件前面都写入这条代码,
但因为这个类所有的方法都是一样要在最前面执行这个方法,
那C#有没有像SQL数据库中的触发器一样的写法,当执行MyClass中的类的时候,先执行这个Validation.Check触发器呢?谢谢public class MyClass
{
    public int A1(int val)
    {
        Validation.Check(val);
        return 1;
    }
    public int A2(int val)
    {
        Validation.Check(val);
        return 2;
    }
    public int A3(int val)
    {
        Validation.Check(val);
        return 3;
    }
    public int A4(int val)
    {
        Validation.Check(val);
        return 4;
    }
    public int A5(int val)
    {
        Validation.Check(val);
        return 5;
    }
    public int A6(int val)
    {
        Validation.Check(val);
        return 6;
    }
}
public class Validation
{
    public static void Check(int val)
    {
        if (val < 100)
        {
            throw new Exception("值不能为100");
        }
    }
}

解决方案 »

  1.   

    在构造方法里面调用Validation.Check不就好了吗?
      

  2.   

    这个可以用事件和委托来实现....
    建议LZ 看看,C#事件委托的机制
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace Delegate {
        // 热水器
        public class Heater {
           private int temperature;
           public string type = "RealFire 001";       // 添加型号作为演示
           public string area = "China Xian";         // 添加产地作为演示
           //声明委托
           public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e);
           public event BoiledEventHandler Boiled; //声明事件       // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息
           public class BoiledEventArgs : EventArgs {
               public readonly int temperature;
               public BoiledEventArgs(int temperature) {
                  this.temperature = temperature;
               }
           }       // 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视
           protected virtual void OnBoiled(BoiledEventArgs e) {
               if (Boiled != null) { // 如果有对象注册
                  Boiled(this, e);  // 调用所有注册对象的方法
               }
           }
           
           // 烧水。
           public void BoilWater() {
               for (int i = 0; i <= 100; i++) {
                  temperature = i;
                  if (temperature > 95) {
                      //建立BoiledEventArgs 对象。
                      BoiledEventArgs e = new BoiledEventArgs(temperature);
                      OnBoiled(e);  // 调用 OnBolied方法
                  }
               }
           }
        }    // 警报器
        public class Alarm {
           public void MakeAlert(Object sender, Heater.BoiledEventArgs e) {
               Heater heater = (Heater)sender;     //这里是不是很熟悉呢?
               //访问 sender 中的公共字段
               Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type);
               Console.WriteLine("Alarm: 嘀嘀嘀,水已经 {0} 度了:", e.temperature);
               Console.WriteLine();
           }
        }    // 显示器
        public class Display {
           public static void ShowMsg(Object sender, Heater.BoiledEventArgs e) {   //静态方法
               Heater heater = (Heater)sender;
               Console.WriteLine("Display:{0} - {1}: ", heater.area, heater.type);
               Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", e.temperature);
               Console.WriteLine();
           }
        }    class Program {
           static void Main() {
               Heater heater = new Heater();
               Alarm alarm = new Alarm();           heater.Boiled += alarm.MakeAlert;   //注册方法
               heater.Boiled += (new Alarm()).MakeAlert;      //给匿名对象注册方法
               heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert);    //也可以这么注册
               heater.Boiled += Display.ShowMsg;       //注册静态方法           heater.BoilWater();   //烧水,会自动调用注册过对象的方法
           }
        }
    }输出为:
    Alarm:China Xian - RealFire 001:
    Alarm: 嘀嘀嘀,水已经 96 度了:
    Alarm:China Xian - RealFire 001:
    Alarm: 嘀嘀嘀,水已经 96 度了:
    Alarm:China Xian - RealFire 001:
    Alarm: 嘀嘀嘀,水已经 96 度了:
    Display:China Xian - RealFire 001:
    Display:水快烧开了,当前温度:96度。
    // 省略 ...
      

  4.   

    你可以创建一方法,方法内部先调用 Check 方法 符合条件利用反射调用相应方法
      

  5.   


    class MyClass
        {
            public void Chk(int val, string methodName)
            {
                Validation.Check(val);
                switch (methodName)
                {
                    case "A1":
                        A1(val);
                        break;
                    case "A2":
                        A2(val);
                        break;
                    case "A3":
                        A3(val);
                        break;
                    case "A4":
                        A4(val);
                        break;
                    case "A5":
                        A5(val);
                        break;
                    case "A6":
                        A6(val);
                        break;
                    default:
                        throw new InvalidOperationException("没有该方法");
                        break;
                }
            }
            public int A1(int val)
            {
                return 1;
            }
            public int A2(int val)
            {
                return 2;
            }
            public int A3(int val)
            {
                return 3;
            }
            public int A4(int val)
            {
                return 4;
            }
            public int A5(int val)
            {
                return 5;
            }
            public int A6(int val)
            {
                return 6;
            }
        }
        class Validation
        {
            public static void Check(int val)
            {
                if (val < 100)
                {
                    throw new Exception("值不能为100");
                }
            }
        }