private AssignStatement SetDelegate(LocalReference localDelegate, ReferenceExpression localTarget, Type closedDelegateType, MethodInfo closedMethodOnTarget) { var delegateCreateDelegate = new MethodInvocationExpression( null, DelegateMethods.CreateDelegate, new TypeTokenExpression(closedDelegateType), localTarget, new MethodTokenExpression(closedMethodOnTarget)); return new AssignStatement(localDelegate, new ConvertExpression(closedDelegateType, delegateCreateDelegate)); }
private FieldReference BuildDelegateToken(ClassEmitter proxy) { var callback = proxy.CreateStaticField(namingScope.GetUniqueName("callback_" + method.Method.Name), delegateType); var createDelegate = new MethodInvocationExpression( null, DelegateMethods.CreateDelegate, new TypeTokenExpression(delegateType), NullExpression.Instance, new MethodTokenExpression(method.MethodOnTarget)); var bindDelegate = new AssignStatement(callback, new ConvertExpression(delegateType, createDelegate)); proxy.ClassConstructor.CodeBuilder.AddStatement(bindDelegate); return callback; }
protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class, ProxyGenerationOptions options, INamingScope namingScope) { var invocationType = invocation; Trace.Assert(MethodToOverride.IsGenericMethod == invocationType.IsGenericTypeDefinition); var genericArguments = Type.EmptyTypes; var constructor = invocation.GetConstructors()[0]; Expression proxiedMethodTokenExpression; if (MethodToOverride.IsGenericMethod) { // bind generic method arguments to invocation's type arguments genericArguments = emitter.MethodBuilder.GetGenericArguments(); invocationType = invocationType.MakeGenericType(genericArguments); constructor = TypeBuilder.GetConstructor(invocationType, constructor); // Not in the cache: generic method proxiedMethodTokenExpression = new MethodTokenExpression(MethodToOverride.MakeGenericMethod(genericArguments)); } else { var proxiedMethodToken = @class.CreateStaticField(namingScope.GetUniqueName("token_" + MethodToOverride.Name), typeof(MethodInfo)); @class.ClassConstructor.CodeBuilder.AddStatement(new AssignStatement(proxiedMethodToken, new MethodTokenExpression(MethodToOverride))); proxiedMethodTokenExpression = proxiedMethodToken.ToExpression(); } var methodInterceptors = SetMethodInterceptors(@class, namingScope, emitter, proxiedMethodTokenExpression); var dereferencedArguments = IndirectReference.WrapIfByRef(emitter.Arguments); var hasByRefArguments = HasByRefArguments(emitter.Arguments); var arguments = GetCtorArguments(@class, proxiedMethodTokenExpression, dereferencedArguments, methodInterceptors); var ctorArguments = ModifyArguments(@class, arguments); var invocationLocal = emitter.CodeBuilder.DeclareLocal(invocationType); emitter.CodeBuilder.AddStatement(new AssignStatement(invocationLocal, new NewInstanceExpression(constructor, ctorArguments))); if (MethodToOverride.ContainsGenericParameters) { EmitLoadGenricMethodArguments(emitter, MethodToOverride.MakeGenericMethod(genericArguments), invocationLocal); } if (hasByRefArguments) { emitter.CodeBuilder.AddStatement(new TryStatement()); } var proceed = new ExpressionStatement(new MethodInvocationExpression(invocationLocal, InvocationMethods.Proceed)); emitter.CodeBuilder.AddStatement(proceed); if (hasByRefArguments) { emitter.CodeBuilder.AddStatement(new FinallyStatement()); } GeneratorUtil.CopyOutAndRefParameters(dereferencedArguments, invocationLocal, MethodToOverride, emitter); if (hasByRefArguments) { emitter.CodeBuilder.AddStatement(new EndExceptionBlockStatement()); } if (MethodToOverride.ReturnType != typeof(void)) { // Emit code to return with cast from ReturnValue var getRetVal = new MethodInvocationExpression(invocationLocal, InvocationMethods.GetReturnValue); emitter.CodeBuilder.AddStatement(new ReturnStatement(new ConvertExpression(emitter.ReturnType, getRetVal))); } else { emitter.CodeBuilder.AddStatement(new ReturnStatement()); } return emitter; }
private Expression SetMethodInterceptors(ClassEmitter @class, INamingScope namingScope, MethodEmitter emitter, Expression proxiedMethodTokenExpression) { var selector = @class.GetField("__selector"); if(selector == null) { return null; } var methodInterceptorsField = BuildMethodInterceptorsField(@class, MethodToOverride, namingScope); var emptyInterceptors = new NewArrayExpression(0, typeof(IInterceptor)); var selectInterceptors = new MethodInvocationExpression(selector, InterceptorSelectorMethods.SelectInterceptors, new MethodInvocationExpression(null, TypeUtilMethods.GetTypeOrNull, getTargetExpression(@class, MethodToOverride)), proxiedMethodTokenExpression, interceptors.ToExpression()) { VirtualCall = true }; emitter.CodeBuilder.AddExpression( new IfNullExpression(methodInterceptorsField, new AssignStatement(methodInterceptorsField, new NullCoalescingOperatorExpression(selectInterceptors, emptyInterceptors)))); return methodInterceptorsField.ToExpression(); }