如何从同一解决方案访问另一个项目?(Visual Studio 2012)



假设我正在制作一个游戏(或类似的东西),解决方案中的第一个项目是级别 1,第二个项目是级别 2,依此类推......想象一下,游戏从1级开始,玩家可以在2级和3级之间做出选择,选择继续的地方。根据他的选择,运行项目 2 或项目 3。我该怎么做(如果可以的话)?我正在使用Visual Studio 2012 Pro,如果它很重要...

如果您需要从"MainProject.exe调用"Project1.exe",这可以帮助您

使用系统诊断;

class Program
{
    static void Main()
    {
        int level ;
        //ask or determine which level wants to be played
        LaunchCommandLineApp(level);
    }
    /// <summary>
    /// Launch the legacy application with some options set.
    /// </summary>
    static void LaunchCommandLineApp(int level)
    {
    // For the example
    const string ex1 = "C:\";
    const string ex2 = "C:\Dir";
    // Use ProcessStartInfo class
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.CreateNoWindow = false;
    startInfo.UseShellExecute = false;
    startInfo.FileName = "Project" + level + ".exe";
    //I guess you dont need this
    //startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    //If your project needs som parameters use this, if not removeit
    startInfo.Arguments = "-f j -o "" + ex1 + "" -z 1.0 -s y " + ex2;
    try
    {
        // Start the process with the info we specified.
        // Call WaitForExit and then the using statement will close.
        using (Process exeProcess = Process.Start(startInfo))
        {
        exeProcess.WaitForExit();
        }
    }
    catch
    {
        // Log error.
    }
    }
}
您可以使用 VS SolutionExplorer -> 项目 1 -在项目

1 中添加对项目 2 的引用,>右键单击References并选择Add Reference,然后指定 dll。

我不完全确定我是否理解您的情况,但是...

有两种选择:

    "主"项目
  1. 引用所有"级别"项目。"主"项目是一个"shell",包含基本的游戏规则逻辑并引用关卡项目。 正是在这里,"你想玩哪个级别?"的问题将发生。
  2. 依赖注入:级别 1 项目查看配置文件并动态加载下一个级别。

最新更新