[英]How to pass arguments in command line using dotnet?
the command dotnet myapp.dll -- [4, 3, 2]
throws the exception System.FormatException: Input string was not in a correct format
. 命令
dotnet myapp.dll -- [4, 3, 2]
引发异常System.FormatException: Input string was not in a correct format
。 I do not know the syntax. 我不知道语法。 How should I pass arguments correctly?
我应该如何正确传递参数? I use powershell.
我使用Powershell。
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Join('-', args));
}
}
}
Call it via Powershell 6: 通过Powershell 6调用它:
dotnet .\ConsoleApp3.dll "[1,2,3]"
Output: 输出:
[1,2,3]
In above call your Main
will receiv [1,2,3]
as single string and you've to parse/split it in your code. 在上面的调用中,
Main
将以单个字符串的形式接收[1,2,3]
,并且您必须在代码中对其进行解析/拆分。
If you want an array reflected in the string[]
-array of Main
you can use a PowerShell array: 如果要在
Main
的string[]
-array中反映一个数组,则可以使用PowerShell数组:
dotnet .\ConsoleApp3.dll @(1,2,3)
Output: 输出:
1-2-3
Here the PowerShell array @(1,2,3)
is casted to a string[]
-array. 在这里,PowerShell数组
@(1,2,3)
被强制转换为string[]
-array。 Therefore each item of the PowerShell array is injected to the string[]
array. 因此,PowerShell数组的每一项都注入到
string[]
数组中。
Behavior is the same on PowerShell 5.1. 行为在PowerShell 5.1上相同。
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.