What you can do is the following:
public class MyClass
{
public MyClass()
{
#if DEBUG
StackTrace trace = new StackTrace();
var callingMethod = trace.GetFrames()[1].GetMethod();
if (callingMethod.IsStatic &&
callingMethod.Name == ".cctor")
{
throw new InvalidOperationException(
"You naughty boy!");
}
#endif
}
}
Static fields will "normally" be created by static constructors. What the above code does, it looks at the calling method to see if it is a static constructor, and if that happens throw an exception.
, , , . , , .