首页 > 解决方案 > Using C#, how can I pass an argument from one project to another

问题描述

I want to debug my MainApp which is a .NET console application which should receive argument from the user. I can right click MainApp>properties>debug and add the required parameter there but this means setting them manually for each test.

I created another project (TESTER) to test MainApp. I added a reference to MainApp in TESTER. How do I call Main, in MainApp from TESTER and pass it arguments programmatically?

MainApp looks like this:

namespace MainApp
{
    public class GetData
    {
        public static void Main(string[] args)

However, from TESTER I can't call Main

var bmsDb = new MainApp.GetData.Main 

is not vailable;

标签: c#

解决方案


如果您知道哪个类MainApp包含 Main 方法(并且该类是公共的),则可以从测试项目中调用它并将参数传递给它,就像调用任何其他静态方法一样。

测试程序集中的 Main 方法应如下所示:

public static void Main() {
    var arguments = new string[] {"arg1", "arg2"};
    MainApp.GetData.Main(arguments);
}

推荐阅读