繁体   English   中英

如何从C#中的另一个类分配ArrayList中的数据

[英]How to assign data in an ArrayList from another class in c#

  1. 如何从C#中的另一个类分配arraylist数据?
  2. 以下是我的课程。
  3. 我想序列化和反序列化数据,但不知道如何在arraylist中分配数据。

 public class PlatanoJson  //this is first class where arraylist is declared  
 {  
    public string question { get; set; }  
    public string correctans { get; set; }  
    public bool typeofans { get; set; }  
    public int indexofque { get; set; }  
    public ArrayList DiffAns = new ArrayList();  
 }  

 public class JsonSerializationController : ApiController  
 {  
    [Route("getjsondata")]  
    public string jsonDataToSerialize()  
    {  
        var game = new PlatanoJson {  
            question = "What is your education?",  
            correctans = "MCA",  
            typeofans = true,  
            indexofque = 3,  
            DiffAns =   //how to add data here??  
        };  

        var result = Newtonsoft.Json.JsonConvert.SerializeObject(game);  

        var dresult = Newtonsoft.Json.JsonConvert.DeserializeObject<PlatanoJson>(result);  

        return (" Question="+dresult.question+" Correct Answer="+dresult.correctans+" Type of ans="+dresult.typeofans+" Index of Que"+dresult.indexofque+" Answers choice="+dresult.DiffAns);
    }
 }

由于ArrayList不会对包含的数据做任何假设,因此可以向其中添加任何数据类型。 为此,有几种可能性,例如使用对象初始化程序:

var game = new PlatanoJson {
    question = "What is your education?",
    correctans = "MCA",
    typeofans = true,
    indexofque = 3,
    DiffAns = new ArrayList { 1, 2, 3, "myString" }
 };

另外,您也可以使用期望ICollection的构造函数:

DiffAns = new ArrayList(new object[] { 1, 2, 3, "myString" })

但是,我建议您花些时间并改用通用方法。 使用ArrayList您无法限制列表中可能包含的对象,可以(如在上面的代码中看到的)将inetegeres,字符串和任何其他数据类型放入其中。 更好地使用List<T> ,它为您提供编译时类型安全性:

var game = new PlatanoJson {
    question = "What is your education?",
    correctans = "MCA",
    typeofans = true,
    indexofque = 3,
    DiffAns = new List<int>{ 1, 2, 3 }  // you van´t put a string into this list
 };

您为什么不将代码更改为以下内容(如果列表包含字符串)。

public class PlatanoJson  //this is first class where arraylist is     declared{
public string question { get; set; }
public string correctans { get; set; }
public bool typeofans { get; set; }
public int indexofque { get; set; }
public List<string> DiffAns { get; set; }
}


 public class JsonSerializationController : ApiController
  {
[Route("getjsondata")]
public string jsonDataToSerialize()
{
var list= new List<string>();
list.Add("your stuff1");
list.Add("your stuff2");
list.Add("your stuff3");
    var game = new PlatanoJson {
           question = "What is your education?",
        correctans = "MCA",
        typeofans = true,
        indexofque = 3,
        DiffAns = list;  
    };
        var result = Newtonsoft.Json.JsonConvert.SerializeObject(game);
    var dresult = Newtonsoft.Json.JsonConvert.DeserializeObject<PlatanoJson>        (......................
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM