Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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/1/list/4.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添加到列表属性#_C#_List - Fatal编程技术网

C# 使用属性索引C添加到列表属性#

C# 使用属性索引C添加到列表属性#,c#,list,C#,List,我想使用foreach循环来添加到c#列表中,而不使用list properties键名 我有一个清单,例如 public class Bus { public string Val1 { get; set; } public string Val2 { get; set; } public string Val3 { get; set; } public string Val4 { get; set; } public string Val5 { ge

我想使用foreach循环来添加到c#列表中,而不使用list properties键名

我有一个清单,例如

public class Bus 
{
    public string Val1 { get; set; }
    public string Val2 { get; set; }
    public string Val3 { get; set; }
    public string Val4 { get; set; }
    public string Val5 { get; set; }
    public string Val6 { get; set; }

    // ...

    public string Val127 { get; set; }
}
我想要填充的列表可以有200多个属性,因此我正在尝试找到一种快速填充它们的方法,而不必写出属性。 我想用这样的东西从一维数组(线)填充它

j = 0
for (int i = 0; i < lines.Length; i++)
{
    foreach(Bus BusProp in BusList)
    {
        BusProp[j] = line[i+j];
        j =+ 1;
    }
}
j=0
对于(int i=0;i
这是行不通的。欢迎提出任何建议

为什么不使用

public class Bus
{
    public string[] Val = new string[127];
}

j = 0;
for (int i = 0; i<lines.Length; i++)
{
    foreach(Bus BusProp in BusList)
    {
        BusProp.Val[j] = line[i + j];
        j =+ 1;
    }
}
公共类总线
{
公共字符串[]Val=新字符串[127];
}
j=0;
对于(inti=0;i为什么不使用

public class Bus
{
    public string[] Val = new string[127];
}

j = 0;
for (int i = 0; i<lines.Length; i++)
{
    foreach(Bus BusProp in BusList)
    {
        BusProp.Val[j] = line[i + j];
        j =+ 1;
    }
}
公共类总线
{
公共字符串[]Val=新字符串[127];
}
j=0;

对于(inti=0;i如果您不能更改类定义,那么您的主要替代选项是使用反射

void Main()
{
  var bus = new Bus();
  var data = new string[6] { "A", "B", "C", "D", "E", "F" };

  for (var i = 1; i <= 6; i++)
  {
    bus.GetType().GetProperty("Val" + i.ToString()).SetValue(bus, data[i - 1]);
  }

  Console.WriteLine(bus.Val5); // E
}

public class Bus 
{
  public string Val1 {get;set;}
  public string Val2 {get;set;}
  public string Val3 {get;set;}
  public string Val4 {get;set;}
  public string Val5 {get;set;}
  public string Val6 {get;set;}
}
void Main()
{
var总线=新总线();
var data=新字符串[6]{“A”、“B”、“C”、“D”、“E”、“F”};

对于(var i=1;i,如果不能更改类定义,则主要的替代选项是使用反射

void Main()
{
  var bus = new Bus();
  var data = new string[6] { "A", "B", "C", "D", "E", "F" };

  for (var i = 1; i <= 6; i++)
  {
    bus.GetType().GetProperty("Val" + i.ToString()).SetValue(bus, data[i - 1]);
  }

  Console.WriteLine(bus.Val5); // E
}

public class Bus 
{
  public string Val1 {get;set;}
  public string Val2 {get;set;}
  public string Val3 {get;set;}
  public string Val4 {get;set;}
  public string Val5 {get;set;}
  public string Val6 {get;set;}
}
void Main()
{
var总线=新总线();
var data=新字符串[6]{“A”、“B”、“C”、“D”、“E”、“F”};
对于(var i=1;i试试这个

var typ=类型(总线)

var prop=typ.GetProperty($“Val{j}”);

试试这个

var typ=类型(总线)


var prop=typ.GetProperty($“Val{j}”);

我觉得到目前为止的答案还不能满足您的需求,因此我的解决方案如下:

    static void Main(string[] args)
    {
        //Create a string array containing the desired property names, in this case I'll use a loop
        List<string> DesiredProperties = new List<string>(); 

        for (int i = 0; i < 100; i++)
        {
            DesiredProperties.Add(string.Format("Property{0}", i));
        }

        //Call the method that returns the object and pass the array as parameter
        var Bus = CreateDynamicObject(DesiredProperties);

        //Display one of the properties
        Console.WriteLine(Bus.Property99);
        Console.Read();
    }
    private static dynamic CreateDynamicObject(List<string> PropertyList)
    {
        dynamic obj = new System.Dynamic.ExpandoObject();
        foreach (string Prop in PropertyList)
        {
            //You can add the properties using a dictionary. You can also give them an initial value
            var dict = (IDictionary<string, object>)obj;
            dict.Add(Prop, string.Format("The value of {0}", Prop));
        }
        return obj;
    }
static void Main(字符串[]args)
{
//创建一个包含所需属性名的字符串数组,在本例中,我将使用循环
List DesiredProperties=新列表();
对于(int i=0;i<100;i++)
{
Add(string.Format(“Property{0}”,i));
}
//调用返回对象并将数组作为参数传递的方法
var Bus=CreateDynamicObject(DesiredProperties);
//显示其中一个属性
控制台写入线(总线属性99);
Console.Read();
}
私有静态动态CreateDynamicObject(列表PropertyList)
{
dynamic obj=新系统.dynamic.ExpandoObject();
foreach(PropertyList中的字符串属性)
{
//您可以使用字典添加属性。您也可以给它们一个初始值
var dict=(IDictionary)obj;
dict.Add(Prop,string.Format)({0}的值,Prop));
}
返回obj;
}

这段代码将为var“Bus”添加100个属性,可以随意访问和应用值。

我觉得到目前为止的答案还不能满足您的需求,因此我的解决方案如下:

    static void Main(string[] args)
    {
        //Create a string array containing the desired property names, in this case I'll use a loop
        List<string> DesiredProperties = new List<string>(); 

        for (int i = 0; i < 100; i++)
        {
            DesiredProperties.Add(string.Format("Property{0}", i));
        }

        //Call the method that returns the object and pass the array as parameter
        var Bus = CreateDynamicObject(DesiredProperties);

        //Display one of the properties
        Console.WriteLine(Bus.Property99);
        Console.Read();
    }
    private static dynamic CreateDynamicObject(List<string> PropertyList)
    {
        dynamic obj = new System.Dynamic.ExpandoObject();
        foreach (string Prop in PropertyList)
        {
            //You can add the properties using a dictionary. You can also give them an initial value
            var dict = (IDictionary<string, object>)obj;
            dict.Add(Prop, string.Format("The value of {0}", Prop));
        }
        return obj;
    }
static void Main(字符串[]args)
{
//创建一个包含所需属性名的字符串数组,在本例中,我将使用循环
List DesiredProperties=新列表();
对于(int i=0;i<100;i++)
{
Add(string.Format(“Property{0}”,i));
}
//调用返回对象并将数组作为参数传递的方法
var Bus=CreateDynamicObject(DesiredProperties);
//显示其中一个属性
控制台写入线(总线属性99);
Console.Read();
}
私有静态动态CreateDynamicObject(列表PropertyList)
{
dynamic obj=新系统.dynamic.ExpandoObject();
foreach(PropertyList中的字符串属性)
{
//您可以使用字典添加属性。您也可以给它们一个初始值
var dict=(IDictionary)obj;
dict.Add(Prop,string.Format)({0}的值,Prop));
}
返回obj;
}

这段代码将向var“Bus”添加100个属性,可以随意访问并应用一个值。

然后将127个字符串属性放在一个数组中,这听起来像是一个非常糟糕的设计。一个类有200个属性?按照单一责任原则将您的类划分为更小的类。127个属性??设计很糟糕,/code。我建议寻找
措辞ary
或类似的数据结构。很可能是糟糕的编码。我正在使用的表可能有250多列。这是一个旧的数据库结构,我不得不使用它。然后将127个字符串属性放在一个数组中,这听起来可能是一个非常糟糕的设计。一个类有200个属性?将您的类划分为更小的类,如下所示ng Single-Responsibility-Principle.127属性??设计糟糕,/code。我建议寻找
字典
或类似的数据结构。很可能是糟糕的编码。我正在使用的表可能有250多列。这是我被迫使用的旧db结构。