C#反射创建List实体

string className = "Person";
string namespaceStr = "ConsoleApplication1";
var model = Assembly.GetExecutingAssembly().CreateInstance(string.Join(".", new object[] { namespaceStr, className }));
var modelList = Activator.CreateInstance(typeof(List<>).MakeGenericType(new Type[] { model.GetType() }));
var addMethod = modelList.GetType().GetMethod("Add");
addMethod.Invoke(modelList, new object[] { model });

posted @ 2017-03-19 21:21  lovewl  阅读(3373)  评论(0编辑  收藏  举报