在运行时从派生类生成对象列表 [英] Generate list of objects from derived classes at run-time

查看:66
本文介绍了在运行时从派生类生成对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我有一个名为Tests.cs的类,它包含一个抽象基类称为测试,然后我编写了更多的类,这些类都来自Test。



在运行时,我希望能够选择这些派生类的实例然后将它们添加到一个列表中,然后保存到磁盘以便在另一个时间召回。



建议



显而易见的方法是对每个Test类的一个实例进行硬编码并将其添加到主列表中,这样在运行时我可以生成一个此列表中的表格,用户可以从中选择制作他们将保存到磁盘的自定义测试对象列表。



这是一个可接受的解决方案(它的工作原理) )但我的期望是在产品生命周期中开发新的测试类,并且为了使这个新软件可以在程序中使用,我必须记住添加一个实例它每次都在主列表中。

我想知道是否有某种方法可以在运行时生成派生测试类的列表,而不依赖于硬编码的类列表实例。



我的尝试:



我试过了创建一个主列表,其中包含从运行时读取的每个测试类的一个实例。这有效,但我想了解是否有更好的方法去处理它。

BACKGROUND

I have a class called "Tests.cs" which contains an abstract base class called "Test" and then I have a bunch more classes written which all derive from Test.

At runtime, I want to be able to choose instances of these derived classes and add them to a list which I then save to disk for recall at another time.

ADVICE

The obvious way to do this is to hard-code one instance of each Test class and add it to a master list, so that at run-time I can generate a table from this list which the user can then choose from to make their custom list of test objects that they will save to disk.

This is an acceptable solution (it works) but my expectation is that new test classes will be developed during the product's life and in order for this new software to be available for use in the program I will have to remember to add an instance of it to the master list each time.
I am wondering if there is some way of generating a list of the derived test classes at run-time which is not reliant upon a hard-coded list of class instances.

What I have tried:

I've tried creating a master list containing one instance of each test class which is read from at run-time. This works but I would like to understand if there are better ways to go about it.

推荐答案

类;



Classes;

public abstract class BaseTest
{
    public abstract string SaySomething();
}

public class Test1 : BaseTest
{
    public override string SaySomething()
    {
        return "Hello from Test1";
    }
}

public class Test2 : BaseTest
{
    public override string SaySomething()
    {
        return "Hello from Test2";
    }
}
    
public class Test3 : BaseTest
{
    public override string SaySomething()
    {
        return "Hello from Test3";
    }
}





代码;





Code;

List<BaseTest> myClasses = new List<BaseTest>();

var myTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(t => t.IsSubclassOf(typeof(BaseTest)));

foreach (Type t in myTypes)
{
    myClasses.Add((BaseTest)Activator.CreateInstance(t));
}

foreach (BaseTest bt in myClasses)
{
    Console.WriteLine("Type is " + bt.GetType().FullName);
    Console.WriteLine(bt.SaySomething());
}





输出





Output

Type is ConsoleApplication1.Test1
Hello from Test1
Type is ConsoleApplication1.Test2
Hello from Test2
Type is ConsoleApplication1.Test3
Hello from Test3


除了 F的解决方案1之外-ES Sitecore [ ^ ],请阅读:

继承(C#编程指南) [ ^ ]

多态性(C#编程指南) [ ^ ]

抽象和密封的类和类成员(C#编程)指南) [ ^ ]

如何:定义抽象属性(C#编程指南) [ ^ ]。
In addition to solution 1 by F-ES Sitecore[^], please read this:
Inheritance (C# Programming Guide)[^]
Polymorphism (C# Programming Guide)[^]
Abstract and Sealed Classes and Class Members (C# Programming Guide)[^]
How to: Define Abstract Properties (C# Programming Guide)[^].


这篇关于在运行时从派生类生成对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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