Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 2-Sep-16 3:23am
v2
Comments
F-ES Sitecore 2-Sep-16 4:13am    
You might need to explain what you're trying to do a bit better. OO, generics and maybe reflection can probably achieve what you want to do, but I don't really understand what that is.
bh_ 2-Sep-16 4:36am    
Thanks very much for the response F-ES Sitecore. I have tried to improve my question. I will look into generics and reflection to see if either of these give me other ideas about how to solve the problem.
F-ES Sitecore 2-Sep-16 5:25am    
See my solution.
bh_ 2-Sep-16 5:43am    
That is a perfect solution, and exactly what I had in mind.

but... where can I learn more about the technique you have employed here?
F-ES Sitecore 2-Sep-16 6:03am    
Hard to answer that, you just pick things up from here and there really. The only complicated bit is loading assemblies, iterating types, creating them etc and as I hinted at before that's called "reflection" so if you google "c# reflection" you'll find out how to discover and manipulate types at run-time (you can find out what properties and methods something has, you can invoke methods etc all using code). The rest is just basic OO principals about base classes and derived classes.

Classes;

C#
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;

C#
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
 
Share this answer
 
Comments
Maciej Los 2-Sep-16 8:53am    
5ed!
BillWoodruff 3-Sep-16 6:55am    
+5 !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900