小编Fet*_*lmi的帖子

条件属性如何在幕后工作

我们有这个代码:

public static class MyCLass 
{
    [Conditional("Debugging")]  
    public static void MyMethod()
    {
         Console.WriteLine("Example method");
    }
}
.
.
.
//In debug mode: Executing Main method in debug mode
MyClass.MyMethod()
Run Code Online (Sandbox Code Playgroud)

我想知道的是条件属性如何改变MyMethod的行为,假设在.NET中Conditional属性定义为:

public class Conditional: Attribute
{
   .
   .
   public string Mode { get; set; )
   .
   .
   public Conditional(string Mode)
   {
       .
       .
       this.Mode = Mode;
       if (Mode == "Debugging")
       {
          #ifdef DEBUG
          //HOW THE CONDITIONAL CONSTRUCTOR COULD CHANGE THE BEHAVIOUR OF MyMethod
          #endif
       }
       .
       .
   }
}
Run Code Online (Sandbox Code Playgroud)

如何访问由我的属性(即来自MyAttribute类)修饰的资源(方法,成员,类......)?

.net c# debugging conditional custom-attributes

1
推荐指数
1
解决办法
107
查看次数

标签 统计

.net ×1

c# ×1

conditional ×1

custom-attributes ×1

debugging ×1