Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a couple of ways to enforce this rule:</p> <ol> <li>Unit Testing - You could write up a unit test (or unit tests) to ensure that the compiled types are passing themselves in as the generic parameter.</li> <li>Code Analysis - You could create a custom code analysis rule that enforces this, and then set that rule as an error (vs warning). This would be checked at compile-time.</li> <li>FxCop Rule - Similar to the Code Analysis rule, except if you don't have a version of Visual Studio that has built-in support for Code Analysis, then you can use FxCop instead.</li> </ol> <p>Of course, none of these rules are enforced on a standard compilation, but instead require additional tools (Unit Testing, Code Analysis, FxCop). If someone took your code and compiled it without using these tools you'd run into the same issue... of course, at that point why is someone else compiling your code without running your unit tests or Code Analysis/FxCop rules?</p> <hr> <p>Alternatively, and I don't recommend this, you could throw a run-time error. Why not? According to Microsoft: </p> <blockquote> <p>If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.</p> </blockquote> <p>That really doesn't solve your issue. On top of that, throwing an exception during static initialization is a violation of Code Analysis <a href="http://msdn.microsoft.com/en-us/library/bb386039.aspx" rel="nofollow">CA1065:DoNotRaiseExceptionsInUnexpectedLocations</a>. So, you're going in the wrong direction if you do this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload