0

AMPL C# API consists in an assembly that contains several enumerations that reside in the global namespace. One of them is an enum named "Type", obviously this is inconvenient because it forces to be explicit when using System.Type.

I'm wondering if there's a way to define a namespace to enclose the whole referenced assembly so that it doesn't clash with the standard libraries.

I don't want to load the assembly dynamically using Assembly.Load() because I would lose intellisense and force me to use reflection.

If there's nothing like that, I figure that I will have to create a proxy assembly that contains the reference to the AMPL API and forward all calls, but that will be annoying.

blit
  • 396
  • 5
  • 12
  • 2
    Maybe ask them to fix that? I would consider that a design flaw. – madreflection Dec 01 '21 at 21:30
  • "define a namespace to enclose the whole referenced assembly so that it doesn't clash with the standard libraries." - there is: Assembly Aliases, it's described in the post I marked this as a dupe-of (even if your question isn't exactly the same) – Dai Dec 01 '21 at 21:32
  • @Dai: So if OP were to alias the AMPL library as `ampl`, they would then have to do `ampl::Type` and such? Seems reasonable until you have to use those types, but I don't think you can do `using ampl::;` to import those types for a single file. You end up having to use the `ampl::` prefix in a lot of places. – madreflection Dec 01 '21 at 21:34
  • @madreflection You don't need to add `using` statements to every page, assembly aliases are project-wide globals (though C#/.NET didn't add support for arbitrary `using` aliases until C# 10.0). – Dai Dec 01 '21 at 21:35
  • @Dai: That's not what I'm saying... I'm saying that even if you limit all your uses of AMPL to a single file, you have the `ampl::` prefix litered throughout that file. You can't do `using ampl::;` to import the non-namespaced types for that file. – madreflection Dec 01 '21 at 21:36
  • @madreflection I think you _can_ use a `using AmplType = global::ampl::Type;` as well (I haven't confirmed that at my end though). – Dai Dec 01 '21 at 21:37
  • @Dai: Certainly, but since everything is behind that prefix, you would have to alias every type you used in that file in the same way. – madreflection Dec 01 '21 at 21:37
  • 1
    @madreflection "you would have to alias every type you used in that file in the same way" - no, just the ambiguous types (like `Type`), assuming other types don't have conflicting names. The rest can be imported with `using ampl; using System; using Type = global::System.Type; using AmplType = ampl::Type;`. – Dai Dec 01 '21 at 21:39
  • I should note that I made a poor choice of `ampl` for an alias/prefix because the assembly also has `ampl` as a root namespace, with a handful of types in it. That may have been confusing. – madreflection Dec 01 '21 at 22:02
  • @Dai: I see what you were saying. The alias I suggested was the source of my confusion because it's the same as a namespace in the assembly (I hadn't downloaded it yet). – madreflection Dec 01 '21 at 22:23
  • But I also tried `global::ampl::Type` and that does *not* work. The alias doesn't exist without `global::`. – madreflection Dec 01 '21 at 22:24
  • I've tried all these suggestions but none of them are legal C# using directives. I will try to contact the developers but I doubt they will listen some random guy. – blit Dec 06 '21 at 20:35

0 Answers0