Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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/2/.net/21.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#_.net_Vb.net_Data Structures - Fatal编程技术网

C# 如何在字典中存储任意数量的任意类型数组

C# 如何在字典中存储任意数量的任意类型数组,c#,.net,vb.net,data-structures,C#,.net,Vb.net,Data Structures,我正在尝试构建一个数据结构,它可以存储我正在运行的一组不同测试的逐个测试结果。这个测试都包含了大量的线索,但是对于不同的测试,我想要保存和以后使用的一些信息是不同的。例如,TestA的结果可能如下所示: Trial(int) WasResponseCorrect(bool) WhichButtonWasPressed(string) SimulusLevel(double) 1 false "U

我正在尝试构建一个数据结构,它可以存储我正在运行的一组不同测试的逐个测试结果。这个测试都包含了大量的线索,但是对于不同的测试,我想要保存和以后使用的一些信息是不同的。例如,TestA的结果可能如下所示:

Trial(int)   WasResponseCorrect(bool)   WhichButtonWasPressed(string)   SimulusLevel(double)

   1                  false                         "Up"                         3.5
   2                  true                          "Left"                       6.5
其中TestB可能具有不同类型的结果字段:

Trial(int)    WasResponseCorrect(bool)    ColorPresented(string)    LetterPresented(char)     LetterGuessed(Char)
  1                     false                      green                     G                        C

  2                     false                      blue                      H                        F

我正在考虑创建一个字典,其中字段名作为键(例如WasResponseCorrect),字段值数组作为dic的值。我不知道该怎么做。也许有更好的方法来存储信息,但我想不出怎么做。我正在使用.net(VB和C#),但如果您知道其他语言的示例,我想我可以理解和转换大多数代码。谢谢

在不了解更多关于您的需求(例如,您将如何存储数据)的情况下,您似乎正在寻找多态性。也就是说,您有一个超类(称为
Trial
)和表示特定试用类型的子类。例如:

public class Trial {
    public int Id { get; set; }
    public bool WasResponseCorrect { get; set; } // if this is in every type of trial
    // anything else that is common to ALL trial types
}

public class TrialA : Trial {
    public string WhichButtonWasPressed { get; set; }
    public double SimulusLevel { get; set; }
}

public class TrialB : Trial {
    public string ColorPresented { get; set; }
    public char LetterPresented { get; set; }
    public char LetterGuessed { get; set; }
}

这样,您就可以拥有一个
Trial
对象列表,但这些对象的实际运行时类型可以是
TrialA
trialab
,而不需要了解更多关于您的需求的信息(例如,您将如何存储数据),看起来多态性就是您要寻找的。也就是说,您有一个超类(称为
Trial
)和表示特定试用类型的子类。例如:

public class Trial {
    public int Id { get; set; }
    public bool WasResponseCorrect { get; set; } // if this is in every type of trial
    // anything else that is common to ALL trial types
}

public class TrialA : Trial {
    public string WhichButtonWasPressed { get; set; }
    public double SimulusLevel { get; set; }
}

public class TrialB : Trial {
    public string ColorPresented { get; set; }
    public char LetterPresented { get; set; }
    public char LetterGuessed { get; set; }
}

这样,您可以有一个
Trial
对象列表,但这些对象的实际运行时类型可以是
TrialA
trialab

只需将信息存储在
数据表中即可。它设计用于存储任意数量的数据行,每个行具有任意数量的列,每个列的类型在编译时都是未知的。我是否遗漏了什么,或者
词汇性是显而易见的答案?这正是我要寻找的!谢谢如果你发布一个答案,我可以接受。@devudef我试过了,但是我以后不能把它们当作正确的类型,除非我把它们扔回去。是的,这会发生。。。在这种情况下,Ethan的答案显然更好。只需将信息存储在
数据表中即可。它设计用于存储任意数量的数据行,每个行具有任意数量的列,每个列的类型在编译时都是未知的。我是否遗漏了什么,或者
词汇性是显而易见的答案?这正是我要寻找的!谢谢如果你发布一个答案,我可以接受。@devudef我试过了,但是我以后不能把它们当作正确的类型,除非我把它们扔回去。是的,这会发生。。。在这种情况下,Ethan的答案显然更好。值得一提的是,只有在编译程序时知道每个试验类型以及这些字段的所有字段和类型时,这才有效。值得一提的是,只有知道每个试验类型以及这些字段的所有字段和类型时,这才有效,在编译程序时。