如何创建类的列表,其中类名取自c#中的字符串 [英] how to create a list of classes where the class name is taken from the string in c#

查看:134
本文介绍了如何创建类的列表,其中类名取自c#中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要问我为什么,但我需要执行以下操作:

string cName =ClassA;

列表< cName> l = new List< cName>();

我该怎么办?

Don't ask me why, but I need to do the following:
string cName = "ClassA";
List<cName> l = new List<cName>();
How can I do it?

我的代码:

public void MergeForm(List< object> lModel)

{ br>
string className = lModel [0] .GetType()。Name;

; className> list = new List< className>();

}

- 它是一个类

my code:
public void MergeForm(List<object> lModel)
{
string className = lModel[0].GetType().Name;
List<className> list = new List<className>();
}
object - it is a class

推荐答案

作为静态类型,但您可以创建实例:

You cannot have List<cName> as static type, but you can create the instance:

IList l = (IList)Activator
    .CreateInstance(typeof(List<>)
    .MakeGenericType(Type.GetType(cName)));

cName 需要是完全限定名,

这篇关于如何创建类的列表,其中类名取自c#中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆