C# 反射 操作列表类型属性

  • 本文介绍对列表进行创建及赋值的反射操作

  • 我们现在有TestA、TestB类,TestA中有TestB类型列表的属性List,如下:.

public class TestA
    {
        public List<TestB> List { get; set; }
    }
    public class TestB
    {
        public TestB(string name)
        {
            Name = name;
        }
        public string Name { get; }
    }
  • 下面通过反射,给TestA.List进行赋值,output的期望是 “1,2”;
var testA = new TestA();
var list = new List<TestB>() { new TestB("1"), new TestB("2") };
AddValueToListProperty(testA, nameof(TestA.List), list);
var output = string.Join(",", testA.List.Select(i => i.Name));

1)确定列表及泛型时,可以直接设置属性值;

private void AddValueToListProperty(object objectValue, string propertyName, List<TestB> list)
{
  var propertyInfo = objectValue.GetType().GetProperty(propertyName);
  propertyInfo.SetValue(objectValue, list, null);
}

2)确定属性是列表,但不确定列表的泛型时,通过列表的Add方式进行设置值;

List<object> list 上方的方案1,是无法进行赋值的,因为类型不一样。会提示隐示转换异常。

private void AddValueToListProperty(object objectValue, string propertyName, List<object> list)
{
        var propertyInfo = objectValue.GetType().GetProperty(propertyName);
        var newList = Activator.CreateInstance(typeof(List<>).MakeGenericType(propertyInfo.PropertyType.GenericTypeArguments));
        propertyInfo.SetValue(objectValue, newList, null);
        var addMethod = newList.GetType().GetMethod("Add");
        foreach (var item in list)
        {
            addMethod.Invoke(newList, new object[] { item });
        }
}

如上,我们需要先创建一个空列表,对属性进行初始化。propertyInfo.PropertyType.GenericTypeArguments是列表的泛型类型;

然后,获取列表的新增方法 newList.GetType().GetMethod("Add") ,将List<object> list一项项添加到列表中。

3)不确定属性是否列表,也不确定列表的泛型,可以如下处理;

private void AddValueToListProperty(object objectValue, string propertyName, object list)
{
        var propertyInfo = objectValue.GetType().GetProperty(propertyName);
        if (typeof(System.Collections.IList).IsAssignableFrom(propertyInfo.PropertyType))
        {
            var newList = Activator.CreateInstance(typeof(List<>).MakeGenericType(propertyInfo.PropertyType.GenericTypeArguments));
            propertyInfo.SetValue(objectValue, newList, null);
            var addMethod = newList.GetType().GetMethod("Add");
            foreach (var item in (IEnumerable)list)
            {
                addMethod.Invoke(newList, new object[] { item });
            }
        }
        else
        {
            propertyInfo.SetValue(objectValue, list, null);
        }
}

如果AddValueToListProperty*方法是设置属性值的通用方法,一般可以按上面的方式进行处理。

当然上面的一些代码是简化后的处理,比如判断是否列表,还需要更严谨的判断见《C# 反射 判断类型是否是列表》