public void Register(Type to, MappedType mappedType, ResolutionMap resolutionMap)
        {
            if(activators.ContainsKey(to)) return;

            var generator = new TypeGenerator();

            var candidates = mappedType.Candidates;
            int candidateCount = candidates.Count;
            for (int candidateCounter = 0; candidateCounter < candidateCount; candidateCounter++)
            {
                var candidate = candidates[candidateCounter];
                if (activators.ContainsKey(candidate.Type)) continue;

                var activatorType = generator.CreateType(context =>
                {
                    context.Named(Guid.NewGuid() + "Builder");
                    context.InheritFrom<SiegeActivator>();

                    context.OverrideMethod<SiegeActivator>(activator => activator.Instantiate(null), method => method.WithBody(body =>
                    {
                        var instance = body.CreateVariable(to);
                        var array = body.CreateArray(typeof(object));
                        array.AssignFromParameter(new MethodParameter(0));
                        var items = new List<ILocalIndexer>();

                        var parameters = candidate.Parameters;
                        var parameterCount = parameters.Count;
                        for (int i = 0; i < parameterCount; i++)
                        {
                            var info = parameters[i];
                            var arg1 = array.LoadValueAtIndex(info.ParameterType, body, info.Position);
                            items.Add(arg1);
                        }

                        var constructorArgs = new Type[candidate.Parameters.Count];
                        var candidateParameters = candidate.Parameters;
                        var candidateParameterCount = candidateParameters.Count;
                        for (int i = 0; i < candidateParameterCount; i++)
                        {
                            var arg = candidateParameters[i];

                            constructorArgs[arg.Position] = arg.ParameterType;
                        }

                        instance.AssignFrom(body.Instantiate(to, constructorArgs, items.OfType<ILocalIndexer, ILocalIndexer>()));
                        body.Return(instance);
                    }));
                });

                var constructor = activatorType.GetConstructor(new Type[] { });

                activators.Add(candidate.Type, (SiegeActivator)constructor.Invoke(new object[] { }));
            }
        }
        public NestedTypeGenerationContext(TypeGenerator generator, Func<BuilderBundle> bundle, Action<BaseTypeGenerationContext> nestedClosure, IList<ITypeGenerationAction> actions)
            : base(generator)
        {
            BaseType = typeof(object);
            TypeGenerationActions = actions;

            nestedType = new DefineNestedTypeAction(bundle(), () => TypeName, () => BaseType);
            TypeGenerationActions.Add(nestedType);

            Builder = () => nestedType.LocalBuilder;

            nestedClosure(this);
            if(!ConstructorAdded) AddDefaultConstructor();
        }
        public NestedTypeGenerationContext(TypeGenerator generator, Func <BuilderBundle> bundle, Action <BaseTypeGenerationContext> nestedClosure, IList <ITypeGenerationAction> actions) : base(generator)
        {
            BaseType = typeof(object);
            TypeGenerationActions = actions;

            nestedType = new DefineNestedTypeAction(bundle(), () => TypeName, () => BaseType);
            TypeGenerationActions.Add(nestedType);

            Builder = () => nestedType.LocalBuilder;

            nestedClosure(this);
            if (!ConstructorAdded)
            {
                AddDefaultConstructor();
            }
        }
示例#4
0
        public TypeGenerationContext(TypeGenerator generator, Func <BuilderBundle> bundle, Action <TypeGenerationContext> nestedClosure) : base(generator)
        {
            BaseType = typeof(object);
            TypeGenerationActions = new List <ITypeGenerationAction>();

            var defineTypeAction = new DefineTypeAction(bundle(), () => TypeName, () => BaseType);

            TypeGenerationActions.Add(defineTypeAction);

            Builder = bundle;

            nestedClosure(this);
            if (!ConstructorAdded)
            {
                AddDefaultConstructor();
            }
        }
示例#5
0
 public BaseTypeGenerationContext(TypeGenerator generator)
 {
     typeGenerator   = generator;
     TypesToComplete = new List <Func <BuilderBundle> >();
 }