예제 #1
0
파일: Clr.cs 프로젝트: cm4ker/XamlIl
        public XamlIlNodeEmitResult Emit(XamlIlEmitContext context, IXamlIlEmitter codeGen)
        {
            var so          = context.Configuration.WellKnownTypes.Object;
            var isp         = context.Configuration.TypeMappings.ServiceProvider;
            var subType     = context.CreateSubType("XamlIlClosure_" + Guid.NewGuid(), so);
            var buildMethod = subType.DefineMethod(so, new[]
            {
                isp
            }, "Build", true, true, false);

            CompileBuilder(new XamlIlEmitContext(buildMethod.Generator, context.Configuration,
                                                 context.RuntimeContext, buildMethod.Generator.DefineLocal(context.RuntimeContext.ContextType),
                                                 (s, type) => subType.DefineSubType(type, s, false), 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)
            {
                codeGen
                .Ldloc(context.ContextLocal)
                .EmitCall(context.Configuration.TypeMappings.DeferredContentExecutorCustomization);
            }

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