C# 处理多个传递的参数
我正在将一个参数传递给一个基于以下代码的程序。代码根据参数值调用特定类的方法。我想对此进行扩展,以允许使用多个参数。需要什么样的代码才能允许传递多个参数并保持当前功能C# 处理多个传递的参数,c#,sql-server,C#,Sql Server,我正在将一个参数传递给一个基于以下代码的程序。代码根据参数值调用特定类的方法。我想对此进行扩展,以允许使用多个参数。需要什么样的代码才能允许传递多个参数并保持当前功能 if (args != null && args.Length > 0) { if (args[0] == "1") { Order.RunOrder(); } if (args[0] == "2") { Shipment.RunShip
if (args != null && args.Length > 0)
{
if (args[0] == "1")
{
Order.RunOrder();
}
if (args[0] == "2")
{
Shipment.RunShipment();
}
if (args[0] == "3")
{
Acknowledgments.RunAcknowledgments();
}
if (args[0] == "4")
{
Invoices.RunInvoices();
}
}
else
{
Helper.AddtoLogFile("------ No program type parameter found");
}
Helper.AddtoLogFile("-------Program Ends ----------");
return 0;
args
只是一个参数数组,因此始终支持多个参数。当前,您正在使用启动应用程序
foo.exe 4
但你也可以从
foo.exe 4 DoSomethingFancyWithBar
然后代码看起来像这样,例如
if (args != null && args.Length > 1)
{
if (args[1] == "DoSomethingFancyWithBar")
{
// do something fancy with the bar here
您可以让每个
SomeClass.DoSomething()
方法使用string[]args
参数。然后,您可以存储除第一个参数以外的所有参数:var argsRest=args.Skip(1).ToList()
(您需要为此使用Linq),然后您可以将参数传递到:SomeClass.DoSomething(argsRest)
这将让您主要处理选择调用哪个类的问题,然后让每个类决定如何处理其余的参数