Custom rule to check null test before using instance member.
-
23 februarie 2012 23:08
Hi,
I would like to write a custom FXCop rule to check if developer has tested if an object is null before calling an instance member like ToString() method or Value property.
Can anyone suggest me how to do that?
Toate mesajele
-
24 februarie 2012 10:00Moderator
If you write something which cannot work as expected, then please post the problem and share us the project.
And if you do not know how to write a FxCop custom rule, then you can ref this tutorial: http://www.thescarms.com/dotnet/FxCop2.aspx
Please try this, since the custom rule logic maybe need more of your time to think.
http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx
http://msdn.microsoft.com/en-us/magazine/hh335064.aspx
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marcat ca răspuns de Mike Dos ZhangMicrosoft Contingent Staff, Moderator 7 martie 2012 10:43
-
24 februarie 2012 11:25
Thanks for your links. I'll take a look on code contracts that seems to do exactly these kind of check even if I'm not using FW 4 and I need to check code "offline" on nightly builds.
I've already developed some custom rule but I can't figure how to check if the test has been done on the variable, for example I don't know if I've to keep track of every test on every variable and when I find some instance member call I've to verify if a test has been done before or if there's another (simpler?) way to achieve my goal.
Another test I would like to include is to check if maximum number of elements in a collection is tested before accessing to an index.
Thanks again.
-
28 februarie 2012 12:20Moderator
I will wait for your response after you researched that framework.
Best wishes,
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Editat de Mike Dos ZhangMicrosoft Contingent Staff, Moderator 28 februarie 2012 12:25
-
29 februarie 2012 13:49
Such a rule would be next to meaningless if the parent instance might be accessed on more than one thread. To make it worthwhile, you would also need to check for adequate concurrency protections. Unfortunately, given the plethora of protection mechanisms available, authoring the rule would be very complex.
-
5 martie 2012 13:52
I downloaded Code Contracts and I saw that there's a static analisys utility that does exactly the same check I would like to create in fxcop. There's also a command line utility (cccheck) that I can use to force the check during nightly builds even if it's pretty slow.
I think that if it's difficult to write down fxcop rules to make those checks this utility should be a nice workaround.