Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .Net StyleCop rule which warns about lock(this), lock(typeof, lock(<string obj>, etc.?

These 3 types of lock are apparently bad. What other type of locking is bad? Are there Stylecop / FxCop rules that would catch this? If not, then would you please help me with a custom rule implementation? They code for all of them must be similar, right?

Thank you.

like image 831
Hamish Grubijan Avatar asked Oct 14 '22 05:10

Hamish Grubijan


1 Answers

The samples (you may need to allow popups in your browser) of John Robbins' Debugging Microsoft .NET Applications book contain sources for such FxCop rules (DoNotLockOnPublicFields, DoNotLockOnThisOrMe, DoNotLockOnTypes, etc.). It looks like they were originally made for FxCop 1.35, whereas the version in VS 2008 and the latest standalone version is 1.36 (not to speak of VS2010). So they might need some tweaking, YMMV.

There is also rule CA2002 (Do not lock on objects with weak identity), which checks for stuff like lock(typeof(...)), but not for lock(this)

like image 113
Christian.K Avatar answered Oct 18 '22 13:10

Christian.K