Ejemplo n.º 1
0
        public XamlILNodeEmitResult Emit(XamlEmitContext <IXamlILEmitter, XamlILNodeEmitResult> context, IXamlILEmitter codeGen)
        {
            var so          = context.Configuration.WellKnownTypes.Object;
            var isp         = context.Configuration.TypeMappings.ServiceProvider;
            var subType     = context.CreateSubType("XamlClosure_" + context.Configuration.IdentifierGenerator.GenerateIdentifierPart(), so);
            var buildMethod = subType.DefineMethod(so, new[]
            {
                isp
            }, "Build", true, true, false);

            CompileBuilder(new ILEmitContext(buildMethod.Generator, context.Configuration,
                                             context.EmitMappings, runtimeContext: context.RuntimeContext,
                                             contextLocal: buildMethod.Generator.DefineLocal(context.RuntimeContext.ContextType),
                                             createSubType: (s, type) => subType.DefineSubType(type, s, false),
                                             defineDelegateSubType: (s, returnType, parameters) => subType.DefineDelegateSubType(s, false, returnType, parameters),
                                             file: context.File,
                                             emitters: context.Emitters));

            var funcType = Type.GetClrType();

            codeGen
            .Ldnull()
            .Ldftn(buildMethod)
            .Newobj(funcType.Constructors.FirstOrDefault(ct =>
                                                         ct.Parameters.Count == 2 && ct.Parameters[0].Equals(context.Configuration.WellKnownTypes.Object)));

            // Allow to save values from the parent context, pass own service provider, etc, etc
            if (context.Configuration.TypeMappings.DeferredContentExecutorCustomization != null)
            {
                var customization = context.Configuration.TypeMappings.DeferredContentExecutorCustomization;
                if (_deferredContentCustomizationTypeParameter != null)
                {
                    customization =
                        customization.MakeGenericMethod(new[] { _deferredContentCustomizationTypeParameter });
                }
                codeGen
                .Ldloc(context.ContextLocal)
                .EmitCall(customization);
            }

            subType.CreateType();
            return(XamlILNodeEmitResult.Type(0, funcType));
        }