Best loose way to get objects with common base class

Posted by Michael Teper on Stack Overflow See other posts from Stack Overflow or by Michael Teper
Published on 2010-05-05T21:01:53Z Indexed on 2010/05/05 21:28 UTC
Read the original article Hit count: 143

Filed under:
|
|

I struggled to come up with a good title for this question, so suggestions are welcome.

Let's say we have an abstract base class ActionBase that looks something like this:

public abstract class ActionBase
{
    public abstract string Name { get; }
    public abstract string Description { get; }

    // rest of declaration follows
}

And we have a bunch of different actions defined, like a MoveFileAction, WriteToRegistryAction, etc. These actions get attached to Worker objects:

public class Worker
{
    private IList<ActionBase> _actions = new List<ActionBase>();
    public IList<ActionBase> Actions { get { return _actions; } }

    // worker stuff ...
}

So far, pretty straight-forward. Now, I'd like to have a UI for setting up Workers, assigning Actions, setting properties, and so on. In this UI, I want to present a list of all available actions, along with their properties, and for that I'd want to first gather up all the names and descriptions of available actions (plus the type) into a collection of the following type of item:

public class ActionDescriptor
{
    public string Name { get; }
    public string Description { get; }
    poblic Type Type { get; }
}

Certainly, I can use reflection to do this, but is there a better way? Having Name and Description be instance properties of ActionBase (as opposed to statics on derived classes) smells a bit, but there isn't an abstract static in C#.

Thank you!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ioc