Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理多个传递的参数_C#_Sql Server - Fatal编程技术网

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)

这将让您主要处理选择调用哪个类的问题,然后让每个类决定如何处理其余的参数