using System;
using System.Reflection;
public class MyObject
{
public class ObjectField
{
public bool field1;
public bool field2;
public bool field3;
}
private void SetObjectField()
{
ObjectField objectField = new ObjectField();
Type type = objectField.GetType();
FieldInfo[] publicFields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var item in publicFields)
{
if (item.Name.Equals("field1"))
{
item.SetValue(objectField, true);
}
}
}
}
C# 根据对象字段名设置字段
于 2024-09-20 11:02:00 首次发布