How do I start a program with arguments when debugging?

前端 未结 6 1934
南笙
南笙 2020-12-04 13:50

I want to debug a program in Visual Studio 2008. The problem is that it exits if it doesn\'t get arguments. This is from the main method:

if (args == null ||         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-04 14:23

    My suggestion would be to use Unit Tests.

    In your application do the following switches in Program.cs:

    #if DEBUG
        public class Program
    #else
        class Program
    #endif
    

    and the same for static Main(string[] args).

    Or alternatively use Friend Assemblies by adding

    [assembly: InternalsVisibleTo("TestAssembly")]
    

    to your AssemblyInfo.cs.

    Then create a unit test project and a test that looks a bit like so:

    [TestClass]
    public class TestApplication
    {
        [TestMethod]
        public void TestMyArgument()
        {
            using (var sw = new StringWriter())
            {
                Console.SetOut(sw); // this makes any Console.Writes etc go to sw
    
                Program.Main(new[] { "argument" });
    
                var result = sw.ToString();
    
                Assert.AreEqual("expected", result);
            }
        }
    }
    

    This way you can, in an automated way, test multiple inputs of arguments without having to edit your code or change a menu setting every time you want to check something different.

提交回复
热议问题