Having multiple Program classes in the global namespace. #7079
-
Hi there. I am confused... I have two projects; one ASP.NET Web API and one CLI console app which consumes the API. Both, of course, have a Program.cs which is written in the new style (without Main method and Program class), which produces a So far so good, but now I want to build a test project which uses the Unfortunately I cannot get this to work successfully since now I have two The only way to solve this is by putting one of the Program.cs contents into a "real" This feels like a loop hole in the C# spec when it comes to top-level statements... |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
You can alias each of the references so that instead of appearing in the using CliProgram = cli::Program;
using AppProgram = app::Program; |
Beta Was this translation helpful? Give feedback.
-
This isn't true. There are various ways to solve this. Firstly though, I'd question why you think this a problem in the first place. That A consequence of this is that you really should see that top level code in // Top level code for your ASP.NET Web API
using StartupCodeNamespace;
WebApiStartUp.Program(args); // Top level code for your console app
using StartupCodeNamespace;
ConsoleAppStartUp.Program(args); And for testing purposes, you then access If for some reason, you are determined to put lots of functionality into those top-level statements, then reflection is another way of solving this: using System.Reflection;
var webApiAassembly = Assembly.LoadFrom("WebApi.dll");
var webApiProgram = webApiAassembly.GetType("Program");
var webApiMain = webApiProgram.GetMethod("<Main>$", BindingFlags.NonPublic|BindingFlags.Static);
var consoleAassembly = Assembly.LoadFrom("Console.dll");
var consoleProgram = consoleAassembly.GetType("Program");
var consoleMain = consoleProgram.GetMethod("<Main>$", BindingFlags.NonPublic|BindingFlags.Static); And then you can call your two |
Beta Was this translation helpful? Give feedback.
-
This should also work (at least with regards to usage via // In Project A Program.cs
public partial class Program;
public class ProgramA : Program;
// In Project B Program.cs
public partial class Program;
public class ProgramB : Program;
// In tests
var programFactoryA = new WebApplicationFactory<ProgramA>();
var programFactoryB = new WebApplicationFactory<ProgramB>(); |
Beta Was this translation helpful? Give feedback.
You can alias each of the references so that instead of appearing in the
global
namespace that they appear in separate namespaces. This is how you can disambiguate classes that have the same fully qualified name in separate assemblies in general: