Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 遍历扩展类列表并动态创建对象_C#_Class_Derived Class - Fatal编程技术网

C# 遍历扩展类列表并动态创建对象

C# 遍历扩展类列表并动态创建对象,c#,class,derived-class,C#,Class,Derived Class,我想遍历扩展类“a”的类列表,并创建扩展类类型的对象 有没有办法用变量替换“new className();”中的类名,或者我必须使用switch语句来创建不同类型的对象 List <A> listOfSubClasses; //A list of classes that all extend "A" List <A> objects; //List to hold created objects int[] variable; foreach (A subClass

我想遍历扩展类“a”的类列表,并创建扩展类类型的对象

有没有办法用变量替换“new className();”中的类名,或者我必须使用switch语句来创建不同类型的对象

List <A> listOfSubClasses; //A list of classes that all extend "A"
List <A> objects; //List to hold created objects
int[] variable; 
foreach (A subClass in listOfSubClasses){
    for (int i = 0; i < 3; i++){ //Let's say I want to create 3 objects of every class
        objects.Add (new subClass()); //This is the line my question refers to
        objects[objects.Count - 1].someParameter = variable[i];
    }
}
子类列表//所有扩展“A”的类的列表
列出对象//用于保存已创建对象的列表
int[]变量;
foreach(ListofSubclass中的子类){
对于(inti=0;i<3;i++){//假设我想为每个类创建3个对象
objects.Add(new subClass());//这是我的问题所指的行
objects[objects.Count-1].someParameter=variable[i];
}
}
您可以使用
列表
存储要实例化的类型,然后使用从该类型创建实例

using System;
using System.Collections.Generic;

public class A
{
    public int someParameter;
}
public class B : A {}
public class C : A {}
public class D : A {}

public class Program
{
    public static void Main()
    {
        List <Type> listOfSubClasses = new List<Type>
        {
            typeof(B),
            typeof(C),
            typeof(D)
        };
        List <A> objects = new List<A>();

        int[] variable = { 1, 2, 3 }; 
        foreach (var subClass in listOfSubClasses) {
            for (int i = 0; i < 3; i++) {
                objects.Add((A)Activator.CreateInstance(subClass));
                objects[objects.Count - 1].someParameter = variable[i];
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
公共A类
{
公共int参数;
}
公共B类:A{}
公共类C:A{}
公共类D:A{}
公共课程
{
公共静态void Main()
{
List of子类=新列表
{
(B)类,
(C)类,
类型(D)
};
列表对象=新列表();
int[]变量={1,2,3};
foreach(ListofSubclass中的var子类){
对于(int i=0;i<3;i++){
Add((A)Activator.CreateInstance(子类));
objects[objects.Count-1].someParameter=variable[i];
}
}
}
}

您可以使用反射。(我尚未在我的机器上检查此解决方案,因此可能存在细微差异。)

使用系统;
// ...
子类列表=
来自AppDomain.CurrentDomain.GetAssemblys()中的程序集
来自程序集中的类型。GetTypes()
其中类型IsubClassof(类型(A))
选择类型;

列表。

子类列表
列出对象都是对象列表您可能需要类似于
List
的东西除了使用
List
来保留要创建实例的(子类)类型之外,还可以考虑利用
Activator.CreateInstance(Type)
满足您的需要……我可以自由地承认我是一个无法容忍的挑剔者(有时),但这里的someProperty实际上是一个字段,而不是属性。我只是觉得很有趣。。。好吧,好吧,我带上外套离开…;-)@几分钟前我注意到了这一点,但我懒得修改它。请随意编辑:)我很高兴地同意并破坏了你的答案;)
using System;

// ...

List<Type> listOfSubClasses =
    from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from type in assembly.GetTypes()
    where type.IsSubclassOf(typeof(A))
    select type;

List<A> objects;
int[] variable; 
foreach (Type subClass in listOfSubClasses) {
    for (int i = 0; i < 3; i++) {
        objects.Add((A)Activator.CreateInstance(subClass));
        objects[objects.Count - 1].someParameter = variable[i];
    }
}