我们有这个代码:
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类)修饰的资源(方法,成员,类......)?