c# - Attribute to generate compilerwarning for a method in class -
i have class, use singleton-pattern. class looks like
public class messageaccess { private static messageaccess instance; public static messageaccess instance { { return instance ?? (instance = new messageaccess()); } } private messageaccess() { } public void initialize(string data) { //... isinitialized = true; } private bool isinitialized; public void readdata1() { // method can called } public void readdata2() { // method can called, if initialize called. otherwise exception thrown } }
is possible generate compiler-warning if method initialize
never called
while understand point of view, don't think such warning handy think. i'm afraid .net framework doesn't cater type of warnings couple of defined reasons (please refer link: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/19/why-doesn-t-c-warn-about-unused-methods.aspx).
one might think lack of feature missed opportunity, it's not quite case. class, messageaccess
, public , compiled (let's say) dll. if had warning while compiling dll, wouldn't want appear while compiling external code using dll's initialize
method (which public). can't guarantee no other code ever use method, , 1 of better reasons not have warning.