1

I have created a class in run time using reflection. I need to create a List collection of my reflection type. How to do that ?

MyClassBuilder MCB = new MyClassBuilder("ReportRecord");
var myclass = MCB.CreateMyProperty(myTable.Columns);
Type TP = myclass.GetType();
var myObject = Activator.CreateInstance(TP);
List<dynamic> ccc = new List<dynamic>();

In the above code I have created a class "ReportRecord" . I need to create collection of ReportRecord that is List.

user3771120
  • 85
  • 10

1 Answers1

3
var listType = typeof(List<>).MakeGenericType(TP);
var list = (IList)Activator.CreateInstance(listType);

Note, however, that this isn't a List<dynamic>; that isn't how "variance" works. You can, however, treat the individual elements as dynamic if it suits you:

foreach(dynamic item in list) {...}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900