Beispiel #1
0
        private MethodInfo CreateNonVirtualCallTrampoline(CodeGenerationContext context, MethodInfo method, string trampolineName)
        {
            var methodDeclaration = MethodDeclaration.CreateEquivalent(method);
            var trampoline        = context.MutableType.AddMethod(
                trampolineName,
                MethodAttributes.Private,
                methodDeclaration,
                ctx => Expression.Call(ctx.This, NonVirtualCallMethodInfoAdapter.Adapt(method), ctx.Parameters.Cast <Expression>()));

            _memberEmitter.AddMethod(context, trampoline);

            return(trampoline);
        }
Beispiel #2
0
        private Action CreateExplicitOverrideBuildAction(CodeGenerationContext context, MutableMethodInfo overridingMethod)
        {
            return(() =>
            {
                var emittableOverridingMethod = context.EmittableOperandProvider.GetEmittableMethod(overridingMethod);

                foreach (var overriddenMethod in overridingMethod.AddedExplicitBaseDefinitions)
                {
                    var emittableOverriddenMethod = context.EmittableOperandProvider.GetEmittableMethod(overriddenMethod);
                    context.TypeBuilder.DefineMethodOverride(emittableOverridingMethod, emittableOverriddenMethod);
                }
            });
        }
Beispiel #3
0
 private Action CreateBodyBuildAction(
     CodeGenerationContext context,
     IMethodBaseBuilder methodBuilder,
     IEnumerable <ParameterExpression> parameterExpressions,
     Expression unpreparedBody)
 {
     // Bodies need to be generated after all other members have been declared (to allow bodies to reference new members).
     return(() =>
     {
         var body = _expressionPreparer.PrepareBody(context, unpreparedBody);
         var bodyLambda = Expression.Lambda(body, parameterExpressions);
         methodBuilder.SetBody(bodyLambda, _ilGeneratorFactory, context.DebugInfoGenerator);
     });
 }
Beispiel #4
0
        private void DefineGenericParameters(CodeGenerationContext context, IMethodBuilder methodBuilder, MutableMethodInfo method)
        {
            if (!method.IsGenericMethodDefinition)
            {
                return;
            }

            var genericParameterNames     = method.MutableGenericParameters.Select(p => p.Name).ToArray();
            var genericParametersBuilders = methodBuilder.DefineGenericParameters(genericParameterNames);

            foreach (var pair in genericParametersBuilders.Zip(method.MutableGenericParameters, (b, g) => new { Builder = b, GenericParameter = g }))
            {
                pair.Builder.RegisterWith(context.EmittableOperandProvider, pair.GenericParameter);
                DefineGenericParameter(pair.Builder, pair.GenericParameter);
            }
        }
Beispiel #5
0
        public MethodInfo GetNonVirtualCallTrampoline(CodeGenerationContext context, MethodInfo method)
        {
            ArgumentUtility.CheckNotNull("context", context);
            ArgumentUtility.CheckNotNull("method", method);

            MethodInfo trampoline;

            if (!context.TrampolineMethods.TryGetValue(method, out trampoline))
            {
                var name = string.Format("{0}.{1}_NonVirtualCallTrampoline", method.DeclaringType.FullName, method.Name);
                trampoline = CreateNonVirtualCallTrampoline(context, method, name);
                context.TrampolineMethods.Add(method, trampoline);
            }

            return(trampoline);
        }
Beispiel #6
0
        public void AddConstructor(CodeGenerationContext context, MutableConstructorInfo constructor)
        {
            ArgumentUtility.CheckNotNull("context", context);
            ArgumentUtility.CheckNotNull("constructor", constructor);

            var parameterTypes = GetParameterTypes(constructor);
            var ctorBuilder    = context.TypeBuilder.DefineConstructor(constructor.Attributes, constructor.CallingConvention, parameterTypes);

            ctorBuilder.RegisterWith(context.EmittableOperandProvider, constructor);

            DefineParameters(ctorBuilder, constructor);
            DefineCustomAttributes(ctorBuilder, constructor);

            var bodyBuildAction = CreateBodyBuildAction(context, ctorBuilder, constructor.ParameterExpressions, constructor.Body);

            context.PostDeclarationsActionManager.AddAction(bodyBuildAction);
        }
Beispiel #7
0
        public void AddEvent(CodeGenerationContext context, MutableEventInfo event_)
        {
            ArgumentUtility.CheckNotNull("context", context);
            ArgumentUtility.CheckNotNull("event_", event_);

            var addMethod    = event_.MutableAddMethod;
            var removeMethod = event_.MutableRemoveMethod;
            var raiseMethod  = event_.MutableRaiseMethod;

            var eventBuilder = context.TypeBuilder.DefineEvent(event_.Name, event_.Attributes, event_.EventHandlerType);

            DefineCustomAttributes(eventBuilder, event_);

            eventBuilder.SetAddOnMethod(context.MethodBuilders[addMethod]);
            eventBuilder.SetRemoveOnMethod(context.MethodBuilders[removeMethod]);
            if (raiseMethod != null)
            {
                eventBuilder.SetRaiseMethod(context.MethodBuilders[raiseMethod]);
            }
        }
 private void WireAndAddConstructor(
     IMemberEmitter member, CodeGenerationContext context, MutableConstructorInfo constructor, Tuple <FieldInfo, MethodInfo> initializationMembers)
 {
     _initializationBuilder.WireConstructorWithInitialization(constructor, initializationMembers, _proxySerializationEnabler);
     member.AddConstructor(context, constructor);
 }