C# 实例化一个列表<;类别>;从文本类名称

C# 实例化一个列表<;类别>;从文本类名称,c#,class,reflection,types,instance,C#,Class,Reflection,Types,Instance,我想知道是否有可能从文本类名实例化实例列表。 例如,我有以下代码: List<Person> Persons; 列出人员; 我希望在为某些对象指定类名时使用这种控件: string ClassName = "Person"; List<ClassName> Persons; string ClassName=“Person”; 列出人员名单; 如果有可能使用反射,请帮助我,谢谢。以下代码将按照您的要求执行-在Linqpad中运行它以查看输出。关键方法是 如果您给出

我想知道是否有可能从文本类名实例化实例列表。 例如,我有以下代码:

List<Person> Persons;
列出人员;
我希望在为某些对象指定类名时使用这种控件:

string ClassName = "Person";
List<ClassName> Persons;
string ClassName=“Person”;
列出人员名单;

如果有可能使用反射,请帮助我,谢谢。

以下代码将按照您的要求执行-在Linqpad中运行它以查看输出。关键方法是

如果您给出了实际的用例或需求,我可以调整代码,使其对您更有用

void Main()
{
    string className = "UserQuery+Person";
    Type personType = Type.GetType(className);
    Type genericListType = typeof(List<>);

    Type personListType = genericListType.MakeGenericType(personType);

    IList personList = Activator.CreateInstance(personListType) as IList;

    // The following code is intended to demonstrate that this is a real
    // list you can add items to. 
    // In practice you will typically be using reflection from this point 
    // forwards, as you won't know at compile time what the types in 
    // the list actually are...
    personList.Add(new Person { Name = "Alice" });
    personList.Add(new Person { Name = "Bob" });

    foreach (var person in personList.Cast<Person>())
    {
        Console.WriteLine(person.Name);
    }
}

class Person
{
    public string Name { get; set;}
}
void Main()
{
string className=“UserQuery+Person”;
Type personType=Type.GetType(类名);
类型genericListType=typeof(列表);
类型personListType=genericListType.MakeGenericType(personType);
IList personList=Activator.CreateInstance(personListType)作为IList;
//下面的代码旨在演示这是一个真实的
//您可以向其中添加项目的列表。
//在实践中,您通常会从这一点使用反射
//转发,因为您在编译时不知道
//这个名单实际上是。。。
添加(新人物{Name=“Alice”});
添加(新人物{Name=“Bob”});
foreach(personList.Cast()中的变量person)
{
Console.WriteLine(person.Name);
}
}
班主任
{
公共字符串名称{get;set;}
}

以下代码将按照您的要求执行-在Linqpad中运行它以查看输出。关键方法是

如果您给出了实际的用例或需求,我可以调整代码,使其对您更有用

void Main()
{
    string className = "UserQuery+Person";
    Type personType = Type.GetType(className);
    Type genericListType = typeof(List<>);

    Type personListType = genericListType.MakeGenericType(personType);

    IList personList = Activator.CreateInstance(personListType) as IList;

    // The following code is intended to demonstrate that this is a real
    // list you can add items to. 
    // In practice you will typically be using reflection from this point 
    // forwards, as you won't know at compile time what the types in 
    // the list actually are...
    personList.Add(new Person { Name = "Alice" });
    personList.Add(new Person { Name = "Bob" });

    foreach (var person in personList.Cast<Person>())
    {
        Console.WriteLine(person.Name);
    }
}

class Person
{
    public string Name { get; set;}
}
void Main()
{
string className=“UserQuery+Person”;
Type personType=Type.GetType(类名);
类型genericListType=typeof(列表);
类型personListType=genericListType.MakeGenericType(personType);
IList personList=Activator.CreateInstance(personListType)作为IList;
//下面的代码旨在演示这是一个真实的
//您可以向其中添加项目的列表。
//在实践中,您通常会从这一点使用反射
//转发,因为您在编译时不知道
//这个名单实际上是。。。
添加(新人物{Name=“Alice”});
添加(新人物{Name=“Bob”});
foreach(personList.Cast()中的变量person)
{
Console.WriteLine(person.Name);
}
}
班主任
{
公共字符串名称{get;set;}
}

听起来像是XY问题,你最终想做什么?听起来像是XY问题,你最终想做什么?如果
className==“someclassnamethanperson”
?您的代码仅适用于
Person
类,而不适用于任何其他类。在
Activator.CreateInstance
之后的所有内容都旨在证明这是一个您可以添加到的真实列表-因为他没有解释他打算如何使用此列表,因此纯粹是为了演示一种使用它的方式。我已经编辑了我的代码来说明这一点。非常感谢@ChrisDunaway,它对我很有帮助,但是是的,它只适用于“Person类”,这里的问题是你在personList上使用“cast”作为书面的“personList.cast”,但是如何在没有硬代码的情况下进行转换呢?再次非常感谢!除非在编译时知道类型,否则不能将其强制转换为编译时类型(显然!!)。您几乎只能使用反射方法来操作列表。如果您给出您的实际要求,我们可以更好地帮助您@RB,我添加了另一个例子,这就是我需要做的,避免硬代码,控制任何对象只需指定类{entity或repository}的名称,我知道我没有解释好,对不起,非常感谢你如果
className==“someclassnameotheranPerson”
?您的代码仅适用于
Person
类,而不适用于任何其他类。在
Activator.CreateInstance
之后的所有内容都旨在证明这是一个您可以添加到的真实列表-因为他没有解释他打算如何使用此列表,因此纯粹是为了演示一种使用它的方式。我已经编辑了我的代码来说明这一点。非常感谢@ChrisDunaway,它对我很有帮助,但是是的,它只适用于“Person类”,这里的问题是你在personList上使用“cast”作为书面的“personList.cast”,但是如何在没有硬代码的情况下进行转换呢?再次非常感谢!除非在编译时知道类型,否则不能将其强制转换为编译时类型(显然!!)。您几乎只能使用反射方法来操作列表。如果您给出您的实际要求,我们可以更好地帮助您@RB,我添加了另一个例子,这就是我需要做的,避免硬代码,控制任何对象只需指定类{entity或repository}的名称,我知道我没有解释好,对不起,非常感谢