C# 如何实现方法注释以在调用方法之前检查某些条件

C# 如何实现方法注释以在调用方法之前检查某些条件,c#,annotations,C#,Annotations,假设我有这个代码 using System; public class Program { class Test { private int someValue = 10; public void MethodA(){ if(someValue == 10){ Console.WriteLine("Method A"); } }

假设我有这个代码

using System;
public class Program
{
    class Test
    {   
        private int someValue = 10;

        public void MethodA(){
            if(someValue == 10){
                Console.WriteLine("Method A");
            }
        }

        public void MethodB(){
            if(someValue == 100){
                Console.WriteLine("Method B");
            }
        }

        public void MethodC(){
            if(someValue == 1000){
                Console.WriteLine("Method C");
            } 
        }
    }

    public static void Main()
    {
        var test = new Test();
        test.MethodA();
        test.MethodB();
        test.MethodC();
    }
}
运行代码,我可以在控制台屏幕上打印“方法A”

但是我想使用方法注释重构类测试。 我希望代码将是

class Test{

    private int someValue = 10;

    [RunWhen(10)]
    public void MethodA(){
        Console.WriteLine("Method A");
    }

    [RunWhen(100)]
    public void MethodB(){
        Console.WriteLine("Method B");
    }

    [RunWhen(1000)]
    public void MethodC(){
        Console.WriteLine("Method C");
    }
}
但我不知道如何实现anonation RunWhen,有人能帮我吗。(事实上,我不确定我们能不能用C#做这件事)


感谢

对于常规方法,如果不使用AOP(面向方面编程),您就无法使用。对于常规方法,如果不使用AOP(面向方面编程),您就无法使用AOP。