Search code examples
c#visual-studio-2013resharperresharper-8.2

Create a VS / R# rule to enforce usage of 'this' when calling non-static methods


On my current project I have to call all non static methods with the this. prefix.

For example:

public void Test(){
    //wrong call
    DoIt();
    //Right call
    this.DoIt();
}
public void DoIt(){
    return "yeah";
}

Now R# says (and it is right) that when I type this., it is a redundant qualifier.

So I switched of that rule, but I want to go one step further: can I create a rule in R# (or VS2013) itself to give me a warning when I don't use this. in calling non-static methods?


Solution

  • You could use https://stylecop.codeplex.com/, which checks this and many other formatting issues. It can be somewhat anal sometimes, so it might be a good idea to disable certain rules like spellchecking (the dictionary is limited and adding words is costing a lot of time) and hungarian notation checking (I feel every second variable is hungarian for stylecop....)

    Additionally, I would recommend to use special comments like autogenerated to turn off stylecop for code generated by tools (like entity framework code first migrations)

    http://shishkin.wordpress.com/2008/07/08/stylecop-how-to-ignore-generated-code/