The problem is that sometimes warnings pick up code that is there by design. So how do we reduce the warning count down to 0, while allowing special exceptions that are there by design?
The answer is by using the #pragma directive.
Kirk Allen has a discussion of this technique on his blog.
Basically you want to wrap the code block where you have code that throws warnings (that is there by design) in a #pragma warning block, like so:
#pragma warning disable WarningNumber
// Code that throws warnings here
#pragma warning restore WarningNumber
Where WarningNumber can be discovered from the output of the compile, where the warning is spit out.
For example, suppose you have the following code:
SmtpMail.SmtpServer = SMTPServer;
MailMessage mailMessage = new MailMessage();
Which generates the following output:
D:\Projects\Test1\EmailNotification.cs(163,6): warning CS0618: 'System.Web.Mail.SmtpMail' is obsolete: 'The recommended alternative is System.Net.Mail.SmtpClient. http://go.microsoft.com/fwlink/?linkid=14202'
D:\Projects\Test1\EmailNotification.cs(165,6): warning CS0618: 'System.Web.Mail.MailMessage' is obsolete: 'The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202'
And you decide that you really don't want to switch to System.Net.Mail.SmtpClient because some legacy component that sends out email works with only System.Web.Mail.SmtpMail. To suppress the warning, you would replace the code with:
#pragma warning disable 0618
SmtpMail.SmtpServer = SMTPServer;
MailMessage mailMessage = new MailMessage();
#pragma warning restore 0618


No comments:
Post a Comment