Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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#_Asp.net_For Loop - Fatal编程技术网

C# 循环遍历名称上具有不同编号的对象变量

C# 循环遍历名称上具有不同编号的对象变量,c#,asp.net,for-loop,C#,Asp.net,For Loop,我有以下课程: public class Employees { public string field1 { get; set; } public string field2 { get; set; } public string field3 { get; set; } public string field4 { get; set; } } 我想更改所有这些成员的值。 所以我可以这样说: Employees.field1 = "ghgf"; Employees.field2 = "ghg

我有以下课程:

public class Employees {
public string field1 { get; set; }
public string field2 { get; set; }
public string field3 { get; set; }
public string field4 { get; set; }
}
我想更改所有这些成员的值。 所以我可以这样说:

Employees.field1 = "ghgf";
Employees.field2 = "ghgf";
Employees.field3 = "ghgf";
Employees.field4 = "ghgf";
        var myEmployees = new Employees();
        var properties = myEmployees.GetType().GetProperties();

        foreach (var field in properties)
        {
            field.SetValue(myEmployees, "NewValue");
        }

        // Print all field's values
        foreach (var item in properties)
        {
            Console.WriteLine(item.GetValue(myEmployees));
        }
但是它很难看。会员人数将是30人,所以这不是一个好办法。。。 我想使用for循环,它运行在所有成员和dynamic上,获取相关字段并更改值。例如:

for(int i=1; i<4; i++) {
var field = "field" + i;
Employees.field(the Var!!) = "fgdfd";
}
我遇到了一个问题,因为字段是在for循环中定义的var。

您可以使用反射的方式以很难(而且不正确,IMO)的方式进行操作。
但是,如果您有30个这样的变量,请改变方法:使用
列表
,或
字典
存储所有字段

如果您确实必须使用反射,您可以这样做:

var employees = new Employees();
var type = employees.GetType();

for (int i = 1; i <= 4; ++i)
    type.GetProperty("field"+i).SetValue(employees, "abcde");

Console.WriteLine(employees.field1); // Prints "abcde"
var employees=新员工();
var type=employees.GetType();

对于(inti=1;i您可以尝试使用反射

Type type = typeof(Employees);
PropertyInfo pi =  this.GetType().GetProperty();
pi.SetField(this, value);
这里是MSDN链接:

尝试这种方法(使用)获取类的所有成员并循环它们

Employees myEmployees = new Employees();
MemberInfo[] members = myType.GetMembers();

for (int i =0 ; i < members.Length ; i++)
{
    // Display name and type of the concerned member.
    Console.WriteLine( "'{0}' is a {1}", members[i].Name, members[i].MemberType);
}
Employees myEmployees=新员工();
MemberInfo[]members=myType.GetMembers();
for(int i=0;i
您可以使用反射这样做:

Employees.field1 = "ghgf";
Employees.field2 = "ghgf";
Employees.field3 = "ghgf";
Employees.field4 = "ghgf";
        var myEmployees = new Employees();
        var properties = myEmployees.GetType().GetProperties();

        foreach (var field in properties)
        {
            field.SetValue(myEmployees, "NewValue");
        }

        // Print all field's values
        foreach (var item in properties)
        {
            Console.WriteLine(item.GetValue(myEmployees));
        }
否则,您可以使用列表、字典或创建新结构,以提高灵活性,并使您能够控制字段的更多属性:

    struct FieldProperties
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public string Type { get; set; }
        ...
    }


    List<FieldProperties> lst = new List<FieldProperties>();
struct字段属性
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共字符串类型{get;set;}
...
}
List lst=新列表();

谢谢,但我不能。必须有30个不同的成员才能在这里使用
列表
。这将使操作更容易,并在
列表中为您提供30个单独的对象。请注意,这会使您的代码更难支持,因为如果您将字段的名称或数量从30更改为31,您将无法使用ay忘记更改循环的代码,编译器不会就此发出警告看起来不错,但我在setValue方法中有一个错误:setValue方法的重载不包含2个参数。可能需要添加更多参数?@TomerA此代码使用.Net 4.6。如果您使用的是旧版本,只需在end:
type.GetProperty(“字段”+i).SetValue(雇员,“abcde”,null);