This blog has moved to Medium

Subscribe via email


Posts tagged ‘Unsharper’

Disable Resharper C# 3 syntax

I love Resharper, and I love C# 3.0, but sometimes they can’t play together.
At Delver we still haven’t purchased enough R# 4 licenses, so until we do, won’t use C# 3 features such as lambdas. This makes working with R# 4 annoying, because every file you open is filled with suggestions and warnings for which you just can’t do anything, because they’re all in C# 3. Another dangerous thing is this – C# 3 has gotten better at deducing generic arguments, so R# 4 will tell you to remove the arguments when not needed, thus bringing compilation errors to people with earlier versions.

The solution: disabling C#3 for Resharper. This can be done for every project – select the project and hit F4, and change Resharper’s “Language Level” setting.

Here is a small piece of code that does this for all your projects, over all yours branches (we have ~70 projects X ~5 active branches).

Warning: This code assumes for simplicity that the per project resharper options file doesn’t contain anything interesting (it’s overwritten from scratch). In the current version, this appears to be true. Also, the solution must be closed before running the exe – otherwise the “.reshaper” files will be deleted when you close it.

using System;
using System.IO;
 
namespace Unsharper
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Usage();
                return;
            }
            string arg = args[0];
            int ver;
            if (!int.TryParse(arg, out ver))
            {
                Usage();
                return;
            }
            if (ver != 2 && ver != 3)
            {
                Usage();
                return;
            }
 
            string config = "<Configuration>n<CSharpLanguageLevel>CSharp" + 
                ver + "0</CSharpLanguageLevel>n</Configuration>";
 
            Console.WriteLine("Finding resharper setting files");
            foreach (string csproj in Directory.GetFiles(".", "*.csproj", SearchOption.AllDirectories))
            {
                string resharperFile = csproj + ".resharper";
                Console.WriteLine("Writing " + resharperFile);
                File.WriteAllText(resharperFile, config);
            }
        }
 
        private static void Usage()
        {
            Console.WriteLine("Usage: unsharper {2/3} - make Resharper use C# 2 or C# 3 syntax");
            Console.WriteLine("This runs over all projects in your current folder");
        }
    }
}