3

C# 10 brought implicit usings.

I globaly like them, but they're causing me a conflict on a particular class called Region as there's a conflict with the Microsoft.Identity.Client.Region class.

There's a nice thread here explaining how to disable implicit usings everywhere: C# 10: Disable Global Using

I don't want to do it globally, I want to do it just on specific classes. Alternatively, a solution that would allow me to disable a particular implicit using (Microsoft.Identity.Client.Region) would also serve my needs.

Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
  • This sounds more like a feature request that should be addressed to Microsoft rather than a question. – Mark Cilia Vincenti Dec 04 '22 at 19:12
  • You can still use your own region class by specifying an alias for it without disabling global namespaces: `using MyRegion = YourProject.YourNamespaces.YourRegion;`. That way, you can avoid conflicts and access your own `Region` class via the alias `MyRegion` (just an example name). – Julian Dec 04 '22 at 19:26

1 Answers1

3

To remove implicit namespace for project completely you can use Using xml element (see also) with Remove attribute:

<ItemGroup>
    <Using Remove="Microsoft.Identity.Client" />
</ItemGroup>

It also provides option to specify Condition attribute but I have not found docs on it for this specific use case.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    There don't need to be specific docs because this works the same as `Condition` does in any other element; general docs [here](https://learn.microsoft.com/visualstudio/msbuild/msbuild-conditions). This is probably not relevant or the OP. – Jeroen Mostert Dec 04 '22 at 19:34
  • Thank you @JeroenMostert, unfortunately it didn't work: "Error CA1724: The type name Region conflicts in whole or in part with the namespace name 'Microsoft.Identity.Client.Region'. Change either name to eliminate the conflict." – Luis Gouveia Dec 04 '22 at 20:33
  • @LuisGouveia try removing the namespace - `Remove="Microsoft.Identity.Client.Region"` – Guru Stron Dec 04 '22 at 20:42
  • Thanks @GuruStron, but still no luck. I've tried both with Microsoft.Identity.Client.Region and Microsoft.Identity.Client. The output is the same. – Luis Gouveia Dec 04 '22 at 21:12
  • Have you opened `[ProjectName].GlobalUsings.g.cs` to verify what assembly is actually included? And also verified that this assembly is not imported somewhere else? Where did you add the ``? You should have a number of `CS0246` in all files that use this assembly, since it can no longer be found, when you have actually removed it from the global include – Jimi Dec 04 '22 at 21:36
  • 1
    @LuisGouveia by the way, this namespace does not look like standard import. What is adding it to global imports (if it is "customly" globally addedd, just remove corresponding `global using`)? What project sdk are you using? – Guru Stron Dec 04 '22 at 22:55
  • Thanks @GuruStron, but there aren't any global usings in my project – Luis Gouveia Dec 05 '22 at 08:34
  • @LuisGouveia can you post a [mre] somewhere? – Guru Stron Dec 05 '22 at 15:07