2 using System.Collections.Generic;
15 public FactoryKey( Type type,
string tag )
23 static Dictionary<FactoryKey, FactoryMethod> constructors =
new Dictionary<FactoryKey, FactoryMethod>();
27 foreach ( var key
in constructors.Keys.FindAll( k => k.type == typeof(
T ) && k.tag == tag ) )
30 constructors[key] = method;
34 FactoryKey newKey =
new FactoryKey( typeof(
T ), tag );
35 constructors.Add( newKey, method );
40 foreach ( var key
in constructors.Keys.FindAll( k => k.type == typeof(
T ) && k.tag == tag ) )
41 constructors.Remove( key );
46 return (
T)FactoryCreate( typeof(
T ), tag );
49 internal static object FactoryCreate( Type type,
string tag )
51 foreach ( FactoryKey key
in constructors.Keys )
53 if ( key.type == type && key.tag == tag )
54 return constructors[key].Invoke();
57 throw new KeyNotFoundException(
"Key " + tag +
" for class " + type.Name +
" was not found." );
static void RemoveFactory< T >(string tag, FactoryMethod method)
delegate object FactoryMethod()
static void AddFactory< T >(string tag, FactoryMethod method)
static T FactoryCreate< T >(string tag)