Evolution/Evolution/Factory.cs
/// <summary>
/// Builds a factory instance given it's type and the genotype it must build
/// </summary>
/// <typeparam name="T">The type of the genotype to be built by the factory</typeparam>
/// <typeparam name="G">The type of the factory</typeparam>
/// <returns></returns>
Builds a factory instance given it's type and the genotype it must build
Builds a factory instance given it ' s type and the genotype it must build
public G BuildFactory<T, G>() where T : IGenotype
        {
            TypeLambda typeLambda;
            if (factories.ContainsKey(typeof (T)))
            {
                typeLambda = factories[typeof (T)];
            }
            else
            {
                Type genericType = typeof (T).GetGenericTypeDefinition();
                Type genericFactory = factories[genericType].FactoryType;
                Type newFactoryType = genericFactory.MakeGenericType(typeof (T).GetGenericArguments());
                typeLambda = new TypeLambda(newFactoryType);
                factories.Add(typeof (T), typeLambda);
            }
            if (typeLambda.Activator == null)
            {
                ConstructorInfo ctor = typeLambda.FactoryType.GetConstructors().First();
                typeLambda.Activator = Activator.GetActivator(ctor);
            }
            Activator.ObjectActivator objectActivator = typeLambda.Activator;
            return (G) objectActivator();
        }
public G BuildFactory < T , G > ( ) where T : IGenotype { TypeLambda typeLambda ; if ( factories . ContainsKey ( typeof ( T ) ) ) { typeLambda = factories [ typeof ( T ) ] ; } else { Type genericType = typeof ( T ) . GetGenericTypeDefinition ( ) ; Type genericFactory = factories [ genericType ] . FactoryType ; Type newFactoryType = genericFactory . MakeGenericType ( typeof ( T ) . GetGenericArguments ( ) ) ; typeLambda = new TypeLambda ( newFactoryType ) ; factories . Add ( typeof ( T ) , typeLambda ) ; } if ( typeLambda . Activator == null ) { ConstructorInfo ctor = typeLambda . FactoryType . GetConstructors ( ) . First ( ) ; typeLambda . Activator = Activator . GetActivator ( ctor ) ; } Activator . ObjectActivator objectActivator = typeLambda . Activator ; return ( G ) objectActivator ( ) ; }
Builds a factory instance given it's type and the genotype it must build
Builds a factory instance given it ' s type and the genotype it must build
None
typeparam
The type of the genotype to be built by the factory
The type of the genotype to be built by the factory
typeparam
The type of the factory
The type of the factory