Visual Studio, Sync namespaces except particular files

Viewed 58

I really like new productivity tool Sync namespaces in Visual Studio 2022 but sometimes there are rare cases when I need to exclude some file or folder when I synchronize namespaces.

Example 1

By convention, DI extensions should be created in the Microsoft.Extensions.DependencyInjection namespace. From the ASP.NET Core Guide :

We recommended that apps follow the naming convention of creating extension methods in the Microsoft.Extensions.DependencyInjection namespace. Creating extension methods in the Microsoft.Extensions.DependencyInjection namespace:

  • Encapsulates groups of service registrations.
  • Provides convenient IntelliSense access to the service.

Example 2

I have .NET Standard 2.0 project where I want to use constructor-based record declaration and according to this solution Testing C# 9.0 in VS2019 - CS0518 IsExternalInit is not defined or imported ... How do I define/import it? I'm adding the following file to my project:

namespace System.Runtime.CompilerServices;

internal static class IsExternalInit { }

Obviously after doing 'Sync namespaces' this file will be changed and the trick will not work.

2 Answers

Found this issue since I have the same problem with Extensions.DependencyInjection.

I would like to mention something that may help you with the external init in that case: There is a nuget package for that internal init: https://www.nuget.org/packages/IsExternalInit.

Hope that at least helps you with that problem.

You said .NET Standard 2.0 project but not confirmed that its CORE or Framework?

VS2022 and its successor will support mainly CORE version preferably. I don't know why but trying to be universal is the primary goal of Microsoft.

Instead creating parallel world they modifying its own.

Try to install / Add framework minimum 4.8.xx else go back to old visual studio IDE.

Related