我有这些元素类:
public class DataclassModeloSD
{
public T001 tablaT001 { get; set; }
public T001W tablaT001W { get; set; }
public TSPAT tablaTSPAT { get; set; }
}
和其他课程:
public class T001
{
public string BUKRS { get; set; }
public string BUTXT { get; set; }
}
public class T001W
{
public string WERKS { get; set; }
public string NAME1 { get; set; }
}
public class TSPAT
{
public string SPART { get; set; }
public string VTEXT { get; set; }
}
请问,如何向List<DataclassModeloSD>
添加元素?
我想用JSON格式填写一个返回列表。
感谢。
答案 0 :(得分:2)
我认为你的意思是创建一个新的List<DataclassModeloSD>
对吗?在这种情况下,您必须创建列表中的元素并使用方法Add()
。像这样:
var yourList = new List<DataclassModeloSD>();
yourList.Add(new DataclassModeloSD(){
tablaT001 = new T001(){
BUKRS = "Foo",
BUTXT = "Bar"
},
tablaT001W = new T001W(){
WERKS= "Foo",
NAME1 = "Bar"
},
tablaTSPAT = new TSPAT(){
SPART = "Foo",
VTEXT = "Bar"
}
});
答案 1 :(得分:2)
你可以在这样的声明中做到这一点,
var list = new List<DataclassModeloSD>()
{
new DataclassModeloSD()
{
tablaT001 = new T001()
{
BUKRS = "asdasd",
BUTXT = "dassdf"
},
tablaT001W = new T001W()
{
NAME1 = "asdfsdf",
WERKS = "sdfsdf'
},
tablaTSPAT = new TSPAT()
{
SPART = "dsfsdf",
VTEXT = "sdfdfg"
}
},
new DataclassModeloSD()
{
tablaT001 = new T001()
{
BUKRS = "hjkhjk",
BUTXT = "dfg"
},
tablaT001W = new T001W()
{
NAME1 = "ghjhj",
WERKS = "jkl'
},
tablaTSPAT = new TSPAT()
{
SPART = "fgh",
VTEXT = "dfg"
}
}
};
或其他类似的方式
var tablaT001 = new T001()
{
BUKRS = "asdasd",
BUTXT = "dassdf"
};
var tablaT001W = new T001W()
{
NAME1 = "asdfsdf",
WERKS = "sdfsdf'
};
var tablaTSPAT = new TSPAT()
{
SPART = "dsfsdf",
VTEXT = "sdfdfg"
};
var dataclassModeloSD = new DataclassModeloSD();
dataclassModeloSD.tablaT001 = tablaT001;
dataclassModeloSD.tablaT001W = tablaT001W;
dataclassModeloSD.tablaTSPAT = tablaTSPAT;
list.Add(dataclassModeloSD);
或添加多个项目的另一个例子
var modeloSD1 = new DataclassModeloSD()
{
tablaT001 = new T001()
{
BUKRS = "asdasd",
BUTXT = "dassdf"
},
tablaT001W = new T001W()
{
NAME1 = "asdfsdf",
WERKS = "sdfsdf'
},
tablaTSPAT = new TSPAT()
{
SPART = "dsfsdf",
VTEXT = "sdfdfg"
}
};
var modeloSD2 = new new DataclassModeloSD()
{
tablaT001 = new T001()
{
BUKRS = "hjkhjk",
BUTXT = "dfg"
},
tablaT001W = new T001W()
{
NAME1 = "ghjhj",
WERKS = "jkl'
},
tablaTSPAT = new TSPAT()
{
SPART = "fgh",
VTEXT = "dfg"
}
};
list.Add(modeloSD1);
list.Add(modeloSD2);
但是请注意:这是非常简单的事情,并且有很多资源可用于此级别的编码。请务必在互联网上搜索,因为您将学到很多东西,而不仅仅是在您理解的时候提出问题。