Reflection alternative for switch on enum in order to select namespace/class

Posted by Am on Stack Overflow See other posts from Stack Overflow or by Am
Published on 2010-03-20T21:25:32Z Indexed on 2010/03/20 22:41 UTC
Read the original article Hit count: 146

Filed under:
|
|

Hi,

I have an interface named IHarvester.

There are 3 implementations of that interface, each under their own namespace:

  1. Google
  2. Yahoo
  3. Bing

A HarvesterManager uses the given harvester. It knows the interface and all 3 implementations.

I want some way of letting the class user say in which harvester it wants to use. And in the code select that implementation, without a switch-case implementation.

Can reflection save my day?

Here is the code bits:

// repeat for each harvester
namespace Harvester.Google
{
    public abstract class Fetcher : BaseHarvester, IInfoHarvester {...}
}


public enum HarvestingSource
{
    Google,
    Yahoo,
    Bing,
}
class HarvesterManager {
    public HarvestingSource PreferedSource {get;set;}
    public HarvestSomthing()
    {
        switch (PreferedSource) .... // awful...
    }
}

Thanks.


I will give my 2 cents of why I want to change this. There several people writing harvesters, I want them to focus only on building more harvesters, without ever needing to update the enum, or at worst, update only the enum.

© Stack Overflow or respective owner

Related posts about c#

Related posts about namespace