.NET多项目集成引用失效问题咨询:保留Main方法场景
Hey there, let's break down why your cross-project references aren't working and walk through concrete fixes to keep your Main methods intact while enabling method calls from your integration project.
The Core Problem
Your existing projects are executable projects (console apps, etc.) with Main methods, which compile to .exe files by default. Unlike class libraries (.dll), executables aren't designed to be reused as dependencies out of the box:
- Internal types/methods (the default access level) are blocked from being accessed by other projects.
- If your integration project is also executable, having multiple
Mainmethods will trigger a compiler error about conflicting entry points.
Step-by-Step Solutions
1. Make Your Project Code Accessible
First, you need to let your integration project see the code in your other projects. You have two options:
Option A: Mark Types/Methods as Public
For any method or class you want to call from the integration project, change its access modifier to public:
// In ProjectA's Program.cs public static void DoCriticalWork() { // Your logic here }
This is the simplest approach for code you intend to reuse across projects.
Option B: Allow Internal Access to the Integration Project
If you don't want to make everything public (to keep your API clean), you can grant your integration project access to internal types. Add this to the .csproj file of each project you're referencing:
<ItemGroup> <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo"> <_Parameter1>YourIntegrationProjectName</_Parameter1> </AssemblyAttribute> </ItemGroup>
Replace YourIntegrationProjectName with the exact name of your integration project (match the name in its .csproj <AssemblyName> tag).
2. Resolve Entry Point Conflicts
If your integration project is an executable (has its own Main method), having multiple Main methods across referenced projects will cause a compile error. Fix this by telling the compiler which Main to use:
Add this to your integration project's .csproj file, replacing YourIntegrationProject.Program with the full namespace and class name of your integration project's entry point:
<PropertyGroup> <StartupObject>YourIntegrationProject.Program</StartupObject> </PropertyGroup>
This forces the compiler to prioritize your integration project's Main as the sole entry point, ignoring the Main methods in referenced projects.
3. Call Methods (Including Main) from the Integration Project
Now you can directly call code from your referenced projects. For example:
// In your integration project's Program.cs using ProjectA; class Program { static void Main(string[] args) { // Call a public/internal method from ProjectA Program.DoCriticalWork(); // Trigger ProjectA's Main method if needed ProjectA.Program.Main(new string[]{}); } }
4. Keep Your Original Projects Runnable
You can still run each original project independently: just right-click the project in Solution Explorer → Set as Startup Project, then hit run. The Main method remains intact, and the project will execute as it did before.
Best Practice Recommendation
If you plan to reuse this logic long-term, consider refactoring:
- Extract reusable code from each executable project into a separate class library project.
- Have both the original executable projects (with their
Mainmethods) and your integration project reference these class libraries.
This avoids the hacky workarounds for referencing executables and keeps your codebase clean and maintainable.
内容的提问来源于stack exchange,提问作者vijay p

