1

I have a generated code file that creates a class and doesn't give it a namespace. Because the code is generated and updated some times I don't want to add any additional code there.

using System;

public class Apartment { ... }

I also have the Apartment class in a namespace (in different dll).

using System;
namespace My.Core.Entities {
  public class Apartment  { ... }
}

Now, I try to use the former one in a unit test (that references to both dlls). I have tried following code to ignore the global class.

using System;
using Apartment = Core.Entities.Apartment;
using GlobApartment = global::Apartment

public class Tests {
  private List<Apartment> _apartments = new List<Apartment>();
}

It gives the error Namespace '<global namespace>' contains a definition conflicting with alias 'Apartment'

I understand why the error happens. The question is if there is any way to ignore the global::Apartment some way in this file (I tried the using GlobApartment thing)? I wouldn't like to change all Apartment to My.Core.Entities.Apartment. Is there any way to use the single term Apartment in the code?

Juho Rutila
  • 2,316
  • 1
  • 25
  • 40
  • Did you try the other way round? Leaving the global Apartment alone and aliasing My.Core.Entities.Apartment to eg "MyApartment" ? – Fildor Jun 28 '19 at 07:34
  • It's too many `Apartment` words. Give alias a better name, e.g. `EntityAppartment`. – Sinatr Jun 28 '19 at 07:35
  • @Fildor and Sinatr, yes, that is one way to go around this. Maybe this question is, at this point, if there is some technical thing in C# with what you could ignore some global class name. – Juho Rutila Jun 28 '19 at 07:40

2 Answers2

3

I have tried using static using statements, doesn't work - the one in global namespace gets preference. I have also tried giving the global type an alias (as shown in the OP) - does not work. The only solution that works is giving the non-global one an alias (as pointed out by Sinatr)

using ApartmentEntity = Core.Entities.Apartment;
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
1

You can't "ignore" just one class, but you can isolate all types from a referenced assembly in a separate top-level namespace and import only what you need into your namespace(s). Give the assembly with the generated Apartment class an extern alias. Then you'll be able to say

extern alias generated ;
using GeneratedApartment = generated::Apartment ;
// or just use generated::Apartment in your code

The generated Apartment will no longer be present in the global:: root namespace and you won't have conflicts.

Anton Tykhyy
  • 19,370
  • 5
  • 54
  • 56
  • But will the codes that depend on the generated class `Apartment` to be in global namespace work? I guess not. – Sнаđошƒаӽ Feb 03 '20 at 08:22
  • What "codes"? OP is writing unit tests, and considered renaming the generated class to `GlobApartment`, so it shouldn't be an issue. – Anton Tykhyy Feb 03 '20 at 08:28
  • By "codes" I meant codes that use this generated class considering it to be present in the global namespace (and I do not want to make any change to such codes, heck, I don't even know the "codes" with such dependency in my solution). BTW, the OP considered aliasing the generated global `Apartment` to `GlobApartment` only in the file that they are writing/changing, but using `extern alias` will cause the generated `Apartment` to be _no longer present in the `global::` root namespace_ as you said. – Sнаđошƒаӽ Feb 03 '20 at 08:41