反射:动态获取类型并用于创建列表错误:是一个变量,但像类型一样使用

问题描述

public class Worker<T>
{
  public void Work(T expected)
  {
     // Get a list of all the properties that the incoming object owns
     PropertyInfo[] objectProperties = typeof(T).GetProperties();

     // Grab the first property
     PropertyInfo property = objectProperties [0];

     // Get the property's type
     string propertyType = property.PropertyType.Name;

     // If this item is a list,then lets recreate the list
     if(propertyType.StartsWith("List"))
     {
       Type listType= a[0].GetType();
       List<listType> myList = new List<listType>();
     }
  }
}

在上面的代码中,以下行是错误的:

List<listType> myList = new List<listType>();

如何使用反射动态创建列表?

我收到错误消息,

“'listType'是一个变量,但其使用方式类似于类型'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)