Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#_List_Class_Loops - Fatal编程技术网

C# 使用列表设置类对象?

C# 使用列表设置类对象?,c#,list,class,loops,C#,List,Class,Loops,我有一个包含n个数据成员的类,如下所示: public class Input { int mode1 {get; set;} int geom2 {get; set;} int type3 {get; set;} int spacing4 {get; set;} int fluid5 {get; set;} int spec6 {get; set;} ... ... ... int data_n {get;

我有一个包含n个数据成员的类,如下所示:

public class Input
{
    int mode1  {get; set;}
    int geom2  {get; set;}
    int type3  {get; set;}
    int spacing4 {get; set;}
    int fluid5 {get; set;}
    int spec6 {get; set;}
    ...
    ...
    ...
    int data_n {get; set;}
} 
我有一个n个int项的填充列表

List dataList=新列表


现在,我想通过迭代或任何其他直接方法从dataList中填充类输入的对象,这将很有帮助。谢谢你

你可以试试这样的东西(对象初始值设定项):


正如其他人所说,您可以通过反射使用
GetProperties
方法来实现这一点,但为什么不使用一种简单的方法来实现呢

Input i = new Input();

i.mode1 = dataList[0];
i.geom2 = dataList[1];
i.type3 = dataList[2];
i.spacing4 = dataList[3];
i.fluid5 = dataList[4];
i.spec6 = dataList[5];
迭代代码

 Layout layout = new Layout();
foreach(int i in dataList)
 {
   layout[i]=dataList[i];
 }

反射在这里很有用,但是如果一个类中有98个属性,那么您肯定应该重新考虑您的设计

var properties = typeof(Input)
                .GetProperties()
                .Where(p => p.PropertyType == typeof(int));

int i = 0;
foreach(var prop in properties)
    prop.SetValue(yourObject, dataList[i++]);

但这并不能保证每个属性都会被正确分配,因为@CodeCaster
GetProperty
方法已经提到过,它不会以特定的顺序返回属性。而且当您有
列表时,无法确定顺序并将值映射到属性,如果您可以使用
字典
,其中
是属性名称,则可以将每个属性设置为相应的值。

您可以使用反射尝试类似的操作:

var properties = typeof(Input).GetProperties();

for(int i = 0; i < properties.Count(); ++i)
{
    properties[i].SetValue(dataList[i]);
}
var properties=typeof(Input).GetProperties();
对于(int i=0;i
我不确定您如何使用属性,但您也可以在类中创建一个列表,然后设置整个列表或添加到该列表中。然后可以使用枚举来跟踪值

大概是这样的:

public class Input
{
    public enum MyValues
    {
        mode1 = 1,
        geom2 = 2,
        ...
    }
    public List<int> Data { get; set; }
}
公共类输入
{
公共枚举值
{
模式1=1,
geom2=2,
...
}
公共列表数据{get;set;}
}

你的意思是
var input=new input{mode1=dataList[i+0],geom2=dataList[i+1],
,其中从0开始的
i
是数据列表中的输入索引,使您能够在一个循环中处理多个?您尝试了什么?是@Nikhil我希望列表填充一个实例。列表中的整数与
输入
类中的字段的顺序相同吗?@CodeCaster,我的真实类包含98个数据成员所以我不能使用它。@Rahul:98个数据成员?你不需要括号来实例化一个
新输入
对象吗?EDIT:我假设你不需要括号,因为R.t的答案也表明了这一点。@DeeMac不,我们不需要括号。请看这里。Thanks@DeeMac没问题,伙计。这肯定不是一个愚蠢的评论。-1为假设ns这是…(@Christos请定义“优雅方式”?)@CodeCaster我不知道
GetProperties
方法不会按特定顺序返回属性。至于优雅的方式,我的意思是使用反射,它将类的所有属性称为input,然后将相应的值分配给每个属性,而不必使用属性的名称蒂蒂。
var properties = typeof(Input).GetProperties();

for(int i = 0; i < properties.Count(); ++i)
{
    properties[i].SetValue(dataList[i]);
}
public class Input
{
    public enum MyValues
    {
        mode1 = 1,
        geom2 = 2,
        ...
    }
    public List<int> Data { get; set; }
}