Tag Archives: visual studio

Static refactoring with Visual Studio regular expressions

Regular expressions are a multi-tool in every developer toolbox. One of the places they can be useful is Quick Find and Quick Replace dialogs in Visual Studio. In this post, I’m going to show you how you can use the power of regular expressions in smart refactoring.

Changing an enum to string

Let’s assume I’d like to change an enum to a string in my application, cause I realized that this property can include a value that the user added by hand and cannot be predefined. Let’s have a look at this enum:

public enum EventType
{
    Unknown = 0,
    Concert = 1,
    Movie = 2
}

Now let’s try to find all the usages of this enum. For that task, I’m using a Quick Find in Visual Studio, Ctrl + F.

Have you noticed a blue rectangle in a Quick Find dialog? It is an option to search with a regular expression. Let’s enable that and switch to the Quick Replace dialog. You can do it with the toggle on the left side or with the Ctrl + H.

I entered EventType\.(\w+) expression that means, that it will start with a EventType string, then a regular dot, which I must escape with \. Next are parenthesis, which means that I’m starting a subexpression group, and /w+ will match a word character one or more times. I’m going to replace it with "$1", which is a standard quotation mark and $1 is a reference to the first group.

And the result is really good:

We can refine this expression a bit and add a name for a group.

By changing (\w+) to  (?<entryType>\w+) we have given a name to the results of this group. We can use it in the Replace With input.

Summary 

Creating a regular expression is a try-and-error process, where you would need to refine your expression a couple of times until it matches what you need. However, it can be really useful and with a manual job like that, can save a lot of time.

I hope that you learned something new today and if so, happy days! 💗 

Visual Studio has now solution filtering

Recently I’ve been working with a solution, that has 85 projects and some of them are really big. Analyzing this amount of data for Visual Studio 2019 and JetBrains Resharper causes difficulties and leads to visible slowdowns. In this huge solution, there are multiple services and websites, but usually, I work with only one at a time. Here’s what I did.

Visual Studio 2019 introduced solution filtering, so now when you open a solution from a directory, you can choose:

Do not load projects

Now you can load only the project you need.

Then you can go and load all project dependencies.

What you end up with is a subset of projects that you need for the main project you are really working on.

Will that stay?

Yes and no 🙂 You can notice that filtering out projects will be saved after you open your solution next time in a normal way. However, that does not cause and change in the repository, so the change will not be visible across the team. That might be a good thing – your settings will not affect the work of others. But what if I’d like to share that setting?

You can go ahead and right-click on the solution and choose Save As Solution Filter.

You will end up with .slnf file, that you can open as any other solution file.

In my solution with 85 projects(not the one on the screenshots), I can feel the difference right away. I strongly recommend you try this if you have a similar situation. Cheers!