I can reproduce this (both in debugging and in release), when executed through the IDE with the option "Debugging" => "Enable Visual Studio Hosting" enabled using below. On the command line, it will print “hello”, where, as through the IDE, it will print “world”. It seems that the IDE makes several different reflections in the attributes.
This expected behavior is not , and you should not rely on this behavior. If you want some specific code to be executed: explicitly call the desired code. To get predictable behavior, disable the "Debug" => "Enable Visual Studio Hosting" option.
using System;
public class MyTestAttribute : Attribute {
public MyTestAttribute() {
Program.text = "world";
}
}
class Program {
public static string text = "hello";
[MyTest]
static void Main() {
Console.WriteLine(text);
Console.ReadKey();
}
}
source
share