多主法 [英] Multiple Main Method

查看:84
本文介绍了多主法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,其中两个入口点是我主要的两个主要方法,但我不知道如何根据选定的主要方法执行程序.
请帮助..

i have created a program in which two entry point i main two main method but i dont know how can i execute program based on selected Main Method.
Please Help..

推荐答案

正如Richard指出的那样,应用程序应具有唯一的入口点 ;说,您可以有两个不同的类,它们公开具有正确签名的方法作为入口点,但这不是一个好习惯,因为它会产生歧义,并且您需要明确告诉编译器将哪个方法用作入口-点.

无论如何,如果您编写类似下面的代码的代码:

As Richard already pointed out, an application should have a unique entry-point; said that, you can have two different classes that exposes a method with the right signature to be an entry point, but it is not a good practice as it generates ambiguity, and you need to explicitly tell the compiler which one to use as entry-point.

Anyway, if you write something like the code snipped below:

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
   class Program1
   {
      static void Main(string[] args)
      {
      }
   }

   class Program2
   {
      static void Main(string[] args)
      {
      }
   }
}



尝试编译应用程序时,由于无法选择Program1.MainProgram2.Main之间的入口点,因此会从编译器中收到错误消息.
要使应用程序编译时没有错误,应打开其属性,并从名为启动对象的组合框中选择两个可能的选项之一.



When you try to compile the application you get an error from the compiler, because it is not able to choose the entry-point between Program1.Main and Program2.Main.
To make the application compile without errors, you should open its properties and from the combo-box named startup object you have to choose one of the two possible ones.


这不是合法的方法之所以要放置两个Main Method,是因为基本上我们知道一个程序有一个入口点,如果一个程序有多个入口点,则会给Compiler造成混乱.无论您遇到什么问题,都可以通过两种方式来实现

如果您使用的是VS编辑器,则可以按照给定的步骤进行操作

解决方案资源管理器中,右键单击您的根项目名称
选择属性
选择应用程序"选项卡
从启动对象组合框选择启动对象
然后编译程序.


如果您使用的是命令提示符,则可以通过这种方式进行编译

This is not a legal way to put two Main Method because basically we know a program has one entry point and if a program has more than one entry point it will create confusion for Compiler. what ever come to your question you can do it by two way

If you are using VS Editor then you can follow the given steps

In Solution Explorer right click on Your Root Project Name
Select Properties
select Application Tab
select startup object from startup object Combobox
and then Compile Program.


and if you are using Command Prompt then you can compile in this way

csc FileName.cs/main:className


有一个Main方法,然后从那里调用2个Pseudo-Main方法中的任何一个,也许具有相同的方法签名.那将是实现您想要做的事的最简单方法.
Have a single Main method and from there call either of 2 Pseudo-Main methods, perhaps with the same method signature. That would be the easiest way to achieve what you are trying to do.


这篇关于多主法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆