Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
104 views
in Technique[技术] by (71.8m points)

c# - How to write a code with expiration date?

I just had this idea for something that I'd love to be able to use:

Let's say I have to fix a bug and I decide to write an ugly code line that fixes the immediate problem - but only because I promise myself that I will soon find the time to perform a proper refactoring.

I want to be able to somehow mark that code line as "Expired in" and add a date - so that if the code is compiled some time after that date there will be a compilation error/warning with a proper message.

Any suggestions? It must be possible to perform - maybe using some complicated #IF or some options in visual studio? I'm using VS 2005 - mainly for C#.

Thanks!

[EDIT]: Wow - never expected this question to raise so much interest :) Thank you all for your answers and for turning this into an interesting debate. I know it's hard to justify using anything like this - and I probably won't use it - but sometimes, when you have to ship a version YESTERDAY and you find yourself compromising on a patchy fix instead - you want to force yourself to fix it in the near future.

I chose MartinStettner's suggestion as the answer because it met my needs - no error on runtime - only during compilation, no need to define new types just for this goal - and it's not limited to a scope of an entire method. Cheers!

question from:https://stackoverflow.com/questions/5222015/how-to-write-a-code-with-expiration-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could write comment lines in the form

// Expires on 2011/07/01

and add a prebuild step which does a solution-wide replace of these lines by something like

#error Code expired on 2011/07/01

for all lines that contain a date before the current day. For this prebuild step you would need to write a short program (probably using regular expressions and some date comparision logic)

This step could also be performed by a VS macro, which allows for easier access to all files fo the solution but has the disadvantage that it must be installed and run on all VS installations where your project is compiled.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...