Everything is achievable through technology

generic factory

public class Factory
private Factory() { }

static readonly Dictionary _dict = new Dictionary();

public static T Create(int id, params object[] args)
Type type = null;
if (_dict.TryGetValue(id, out type))
return (T)Activator.CreateInstance(type, args);

throw new ArgumentException("No type registered for this id");

public static void Register(int id) where Tderived : T
var type = typeof(Tderived);

if (type.IsInterface || type.IsAbstract)
throw new ArgumentException("...");

_dict.Add(id, type);


// this is the "suspicious" part.
// some instances may require different parameters?
Factory.Create(2, "Garfield");

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s