当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to warn on bad parameter numbers within Console.Write()?

问题描述:

We have a program with thousands of lines such as:

Console.Write("{0},{1} - {2}", e1, "My custom error", e2); // Write a custom error

However, the following will throw an error, as "{3}" is greater than the number of parameters:

Console.Write("{0},{1} - {3}", e1, "My custom error", e2); // Write a custom error

How can I scan my entire project to pick this up at compile time?

Update:

Unfortunately, ReSharper won't work. I've created my own logging class, "w", which calls Console.Write and other logging behind the scenes. ReSharper picks up errors in "Console.Write", but ignores any parameters for said logging method.

Update:

FXCop doesn't pick up this sort of error. That's surprising - why not pick up an obvious error which will result in the entire program crashing with an exception?

Update:

Thanks for the tip @adudley - I ended up replacing all instances of "Console.Write" and "String.Format", in the logging class, with calls to specialized versions with try/catch for any errors. This avoids the entire program falling over if a single, rarely used logging function is called with bad parameters. I'd rather tell the program to ensure its bulletproof, rather than manually check 1000 logging functions for difficult-to-spot errors.

网友答案:

you've got to ask why would they be wrong? They should be correct at the time of typing, and if you translate them, check when importing translations (that's what we do).

网友答案:

The commercial ReSharper plugin automatically finds exactly that error (I think you need to turn on project-wide inspection though). A free trial exists.

网友答案:

I'm guessing it's probably some huge, un-tested (possibly un-testable!) legacy app but I'll throw a word in for unit tests (eg NUnit) - you can use them with a good test-doubles library (like Rhino.Mocks) to throw various errors and exercise all these lines. The errors will show up as test failures until fixed :)

网友答案:

Static code analysis can do that for you. If you have at least the Premium version of Visual Studio, you can enable it in your project settings. Otherwise you can use FxCop to check your assemblies after build.

Both tools have a built-in rule, that should meet your requirement: Provide correct arguments to formatting methods

网友答案:

I guess this is a perfect opportunity for a bit of refactoring (if you can take the time to do it, of course - if there are thousands of instances of this, it may take a while ;) )

Instead of doing the same Write operation in thousands of places, why not create a static method that will output the error message, for example...

public static void ShowError(string e1, string e2)
{
    // possibly check for null / empty parameters if you need to.
    Console.Write("{0},{1} - {2}", e1, "My custom error", e2);
}

and replace the instances with

ShowError(e1, e2);

This gives you the flexibility to do more in the method than just outputting to the console - you may want to log the error as well... You then only need to update a single method to output in a different format / to another location.

Additionally, you will get compile errors if you try to call the method with an incorrect number / type of parameters.

分享给朋友:
您可能感兴趣的文章:
随机阅读: