I need to create a generic list of T by reflection and add item of T to it by invoke a method "Add", but I have error. this is my code:
public class myObject
{
public object FieldValue { get; set; }
public int ID_OpenField { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
Type type = typeof(List<myObject>);
var genericType = typeof(List<>).MakeGenericType(type);
var list = Activator.CreateInstance(genericType);
myObject obj = new myObject() { FieldValue = "TextObjectValue", ID_OpenField = 1 };
object[] o = new object[] { obj };
list.GetType().GetMethod("Add").Invoke(list, o);
}
the last line gives me this error:
System.ArgumentException: 'Object of type 'myObject' cannot be converted to type 'System.Collections.Generic.List`1[myObject]'.'