gpt4 book ai didi

C# ConsoleRead() 输入未保存在变量中

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:34 25 4
gpt4 key购买 nike

我是第一次使用 C#,我遇到了一个奇怪的问题。

我正在为一个插件构建自己的类,但从现有类中复制了部分代码。基本上是

var sanInput = Console.ReadLine();
alternativeNames = sanInput.Split(',');
sanList = new List<string>(alternativeNames);

但不知何故这不起作用。调试控制台显示 System.Console.ReadLine 返回了 jogi,philipp 字符串,但是 sanInput 保留了 null 作为它的值.

更奇怪的是,下一步“有点”有效。 string.Split 返回了{string[2]} string[],所以它返回了一个[jogi, philipp]的数组,但是还是sanInput, alternativeNamessanList 保持为空。

如果 sanInput 没有值,第二行怎么可能工作,我该如何解决这个问题?当我使用相同代码处理现有类时,一切都按预期工作。

//编辑:看起来是一个相当复杂的问题。这是完整的方法:

    public override void HandleMenuResponse(string response, List<Target> targets)
{

if (response == "r")
{
Console.WriteLine("Which hosts do you want to configure? Enter numbers separated by a comma.");
var hostsInput = Console.ReadLine();
int[] hosts = null;

string[] alternativeNames = null;
List<string> sanList = null;

hosts = hostsInput.Split(',').Select(int.Parse).ToArray();
Console.Write("Generating certificates for ");
foreach (int entry in hosts)
{
Console.Write(targets[entry - 1].Host + ", ");
}
Console.Write("\n \n");

foreach (int entry in hosts)
{
int entry2 = entry - 1;

if (Program.Options.San)
{
Console.WriteLine("Enter all Alternative Names for " + targets[entry2].Host + " seperated by a comma:");
// Copied from http://stackoverflow.com/a/16638000
int BufferSize = 16384;
Stream inputStream = Console.OpenStandardInput(BufferSize);
Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, BufferSize));

var sanInput = Console.ReadLine();
alternativeNames = sanInput.Split(',');
sanList = new List<string>(alternativeNames);
targets[entry2].AlternativeNames.AddRange(sanList);
}

Auto(targets[entry - 1]);
}
}


if (response == "e")
{
string[] alternativeNames = null;
List<string> sanList = new List<string>();

if (Program.Options.San)
{
Console.WriteLine("Enter all Alternative Names seperated by a comma:");
// Copied from http://stackoverflow.com/a/16638000
int BufferSize = 16384;
Stream inputStream = Console.OpenStandardInput(BufferSize);
Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, BufferSize));
var sanInput = Console.ReadLine();
alternativeNames = sanInput.Split(',');
}

if (alternativeNames != null)
{
sanList = new List<string>(alternativeNames);
}

foreach (var entry in targets)
{
Auto(entry);
}
}
}

我知道代码不够漂亮和高效。总而言之,它让用户决定他是想使用所有检测到的主机(响应 e)还是仅使用单个主机(响应 r)。但是上述问题出现在第二个if-method中。如果我切换它们,它又是后者。所以也许原因在于主程序或这个 BufferSize-Stuff?我不知道。

//编辑 2: 我想我发现了问题:整数 BufferSize(在 Console.Read() 之前不久)设置为 0,所以当然没有任何缓冲区它无法读取输入。所以问题仍然存在:为什么?

//编辑 3: 好的,我完成了。看起来我不能对变量使用相同的名称,尽管它们在两个不同的 if 方法中。我只是将它们命名为 sanInput2、alternativeNames2 等,现在一切正常。

最佳答案

试试这个,所有变量都有值(你也可以对所有变量使用 var):

   var sanInput = Console.ReadLine();
string[] alternativeNames = sanInput.Split(',');
List<string> sanList = new List<string>(alternativeNames);

关于C# ConsoleRead() 输入未保存在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com