コード例 #1
0
			public void DoWork(AdapterBuilderStageContext context)
			{
				//First declare some locals (with type IDictionary) for each converted parameter
				List<int> convertedArgs = context["IDArgRefs"] as List<int>;
				List<LocalReference> locals = convertedArgs.ConvertAll<LocalReference>(delegate(int aref)
				{
					return context.NewMethodEmitter.CodeBuilder.DeclareLocal(typeof(IDictionary));
				});

				int j = 0;
				//For each converted parameter
				foreach (int index in convertedArgs)
				{
					//Invoke the GetDictionary method on the relevant parameter and assign the result to the local
					MethodInvocationExpression invocation = 
						new MethodInvocationExpression(SelfReference.Self,
							_GetDictionaryMethod,
							new ReferenceExpression(context.NewMethodEmitter.Arguments[index]));
					//Virtual call
					invocation.VirtualCall = true;
					context.NewMethodEmitter.CodeBuilder.AddStatement(
						new AssignStatement(locals[j++], invocation));

				}
				//Store the local reference for usage in other stages
				context["IDLocalRefs"] = locals;

			}
コード例 #2
0
		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));
		}
コード例 #3
0
		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;
		}
コード例 #4
0
		protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class, ProxyGenerationOptions options,INamingScope namingScope)
		{
			var invocationType = invocation;

			Trace.Assert(MethodToOverride.IsGenericMethod == invocationType.IsGenericTypeDefinition);
			Type[] genericMethodArgs = Type.EmptyTypes;

			ConstructorInfo constructor = invocation.GetConstructors()[0];


			Expression proxiedMethodTokenExpression;
			if (MethodToOverride.IsGenericMethod)
			{
				// bind generic method arguments to invocation's type arguments
				genericMethodArgs = emitter.MethodBuilder.GetGenericArguments();
				invocationType = invocationType.MakeGenericType(genericMethodArgs);
				constructor = TypeBuilder.GetConstructor(invocationType, constructor);

				// Not in the cache: generic method
				proxiedMethodTokenExpression = new MethodTokenExpression(MethodToOverride.MakeGenericMethod(genericMethodArgs));
			}
			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 dereferencedArguments = IndirectReference.WrapIfByRef(emitter.Arguments);


			var ctorArguments = GetCtorArguments(@class, namingScope, proxiedMethodTokenExpression, dereferencedArguments);

			var invocationLocal = emitter.CodeBuilder.DeclareLocal(invocationType);
			emitter.CodeBuilder.AddStatement(new AssignStatement(invocationLocal,
			                                                     new NewInstanceExpression(constructor, ctorArguments)));

			if (MethodToOverride.ContainsGenericParameters)
			{
				EmitLoadGenricMethodArguments(emitter, MethodToOverride.MakeGenericMethod(genericMethodArgs), invocationLocal);
			}

			emitter.CodeBuilder.AddStatement(
				new ExpressionStatement(new MethodInvocationExpression(invocationLocal, InvocationMethods.Proceed)));

			GeneratorUtil.CopyOutAndRefParameters(dereferencedArguments, invocationLocal, MethodToOverride, emitter);

			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;
		}
コード例 #5
0
		private void EmitCustomGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference serializationInfo)
		{
			var members = codebuilder.DeclareLocal(typeof(MemberInfo[]));
			var data = codebuilder.DeclareLocal(typeof(object[]));

			var getSerializableMembers = new MethodInvocationExpression(
				null,
				FormatterServicesMethods.GetSerializableMembers,
				new TypeTokenExpression(targetType));
			codebuilder.AddStatement(new AssignStatement(members, getSerializableMembers));

			// Sort to keep order on both serialize and deserialize side the same, c.f DYNPROXY-ISSUE-127
			var callSort = new MethodInvocationExpression(
				null,
				TypeUtilMethods.Sort,
				members.ToExpression());
			codebuilder.AddStatement(new AssignStatement(members, callSort));

			var getObjectData = new MethodInvocationExpression(
				null,
				FormatterServicesMethods.GetObjectData,
				SelfReference.Self.ToExpression(),
				members.ToExpression());
			codebuilder.AddStatement(new AssignStatement(data, getObjectData));

			var addValue = new MethodInvocationExpression(
				serializationInfo,
				SerializationInfoMethods.AddValue_Object,
				new ConstReference("__data").ToExpression(),
				data.ToExpression());
			codebuilder.AddStatement(new ExpressionStatement(addValue));
		}
コード例 #6
0
		private void GenerateSerializationConstructor(ClassEmitter emitter)
		{
			var serializationInfo = new ArgumentReference(typeof(SerializationInfo));
			var streamingContext = new ArgumentReference(typeof(StreamingContext));

			var ctor = emitter.CreateConstructor(serializationInfo, streamingContext);

			ctor.CodeBuilder.AddStatement(
				new ConstructorInvocationStatement(serializationConstructor,
				                                   serializationInfo.ToExpression(),
				                                   streamingContext.ToExpression()));

			foreach (var field in serializedFields)
			{
				var getValue = new MethodInvocationExpression(serializationInfo,
				                                              SerializationInfoMethods.GetValue,
				                                              new ConstReference(field.Reference.Name).ToExpression(),
				                                              new TypeTokenExpression(field.Reference.FieldType));
				ctor.CodeBuilder.AddStatement(new AssignStatement(
				                              	field,
				                              	new ConvertExpression(field.Reference.FieldType,
				                              	                      typeof(object),
				                              	                      getValue)));
			}
			ctor.CodeBuilder.AddStatement(new ReturnStatement());
		}
コード例 #7
0
		protected void CreateIInvocationInvokeOnTarget(
			ClassEmitter targetTypeEmitter,
			NestedClassEmitter nested,
			ParameterInfo[] parameters,
			FieldReference targetField,
			MethodInfo callbackMethod)
		{
			const MethodAttributes methodAtts = MethodAttributes.Public | MethodAttributes.Final | MethodAttributes.Virtual;

			MethodEmitter method =
				nested.CreateMethod ("InvokeMethodOnTarget", methodAtts, typeof (void));

			Expression[] args = new Expression[parameters.Length];

			// Idea: instead of grab parameters one by one
			// we should grab an array
			Hashtable byRefArguments = new Hashtable();

			for(int i = 0; i < parameters.Length; i++)
			{
				ParameterInfo param = parameters[i];

				Type paramType = param.ParameterType;

				if (HasGenericParameters(paramType))
				{
					paramType = paramType.GetGenericTypeDefinition().MakeGenericType(nested.GetGenericArgumentsFor(paramType));
				}
				else if (paramType.IsGenericParameter)
				{
					paramType = nested.GetGenericArgument(paramType.Name);
				}

				if (paramType.IsByRef)
				{
					LocalReference localReference = method.CodeBuilder.DeclareLocal(paramType.GetElementType());
					method.CodeBuilder.AddStatement(
						new AssignStatement(localReference,
						                    new ConvertExpression(paramType.GetElementType(),
						                                          new MethodInvocationExpression(SelfReference.Self,
						                                                                         typeof(AbstractInvocation).GetMethod(
						                                                                         	"GetArgumentValue"),
						                                                                         new LiteralIntExpression(i)))));
					ByRefReference byRefReference = new ByRefReference(localReference);
					args[i] = new ReferenceExpression(byRefReference);
					byRefArguments[i] = localReference;
				}
				else
				{
					args[i] =
						new ConvertExpression(paramType,
						                      new MethodInvocationExpression(SelfReference.Self,
						                                                     typeof(AbstractInvocation).GetMethod("GetArgumentValue"),
						                                                     new LiteralIntExpression(i)));
				}
			}

			MethodInvocationExpression baseMethodInvExp;

			if (callbackMethod.IsGenericMethod)
			{
				callbackMethod = callbackMethod.MakeGenericMethod(nested.GetGenericArgumentsFor(callbackMethod));
			}

			baseMethodInvExp = new MethodInvocationExpression(targetField, callbackMethod, args);
			baseMethodInvExp.VirtualCall = true;

			LocalReference ret_local = null;

			if (callbackMethod.ReturnType != typeof(void))
			{
				if (callbackMethod.ReturnType.IsGenericParameter)
				{
					ret_local = method.CodeBuilder.DeclareLocal(nested.GetGenericArgument(callbackMethod.ReturnType.Name));
				}
				else if (HasGenericParameters(callbackMethod.ReturnType))
				{
					ret_local =
						method.CodeBuilder.DeclareLocal(
							callbackMethod.ReturnType.GetGenericTypeDefinition().MakeGenericType(
								nested.GetGenericArgumentsFor(callbackMethod.ReturnType)));
				}
				else
				{
					ret_local = method.CodeBuilder.DeclareLocal(callbackMethod.ReturnType);
				}

				method.CodeBuilder.AddStatement(new AssignStatement(ret_local, baseMethodInvExp));
			}
			else
			{
				method.CodeBuilder.AddStatement(new ExpressionStatement(baseMethodInvExp));
			}

			foreach(DictionaryEntry byRefArgument in byRefArguments)
			{
				int index = (int) byRefArgument.Key;
				LocalReference localReference = (LocalReference) byRefArgument.Value;
				method.CodeBuilder.AddStatement(
					new ExpressionStatement(
						new MethodInvocationExpression(SelfReference.Self,
						                               typeof(AbstractInvocation).GetMethod("SetArgumentValue"),
						                               new LiteralIntExpression(index),
						                               new ConvertExpression(typeof(object), localReference.Type,
						                                                     new ReferenceExpression(localReference)))
						));
			}

			if (callbackMethod.ReturnType != typeof(void))
			{
				MethodInvocationExpression setRetVal =
					new MethodInvocationExpression(SelfReference.Self,
					                               typeof(AbstractInvocation).GetMethod("set_ReturnValue"),
					                               new ConvertExpression(typeof(object), ret_local.Type, ret_local.ToExpression()));

				method.CodeBuilder.AddStatement(new ExpressionStatement(setRetVal));
			}

			method.CodeBuilder.AddStatement(new ReturnStatement());
		}
コード例 #8
0
		protected MethodEmitter ImplementProxiedMethod(
			Type targetType,
			MethodEmitter methodEmitter,
			MethodInfo method,
			ClassEmitter emitter,
			NestedClassEmitter invocationImpl,
			FieldReference interceptorsField,
			Reference targetRef,
			ConstructorVersion version,
			MethodInfo methodOnTarget)
		{
			CheckNotGenericTypeDefinition(targetType, "targetType");

			methodEmitter.CopyParametersAndReturnTypeFrom(method, emitter);

			TypeReference[] dereferencedArguments = IndirectReference.WrapIfByRef(methodEmitter.Arguments);

			Type iinvocation = invocationImpl.TypeBuilder;

			Trace.Assert(method.IsGenericMethod == iinvocation.IsGenericTypeDefinition);
			bool isGenericInvocationClass = false;
			Type[] genericMethodArgs = new Type[0];
			if (method.IsGenericMethod)
			{
				// bind generic method arguments to invocation's type arguments
				genericMethodArgs = methodEmitter.MethodBuilder.GetGenericArguments();
				iinvocation = iinvocation.MakeGenericType(genericMethodArgs);
				isGenericInvocationClass = true;
			}

			LocalReference invocationImplLocal = methodEmitter.CodeBuilder.DeclareLocal(iinvocation);

			// TODO: Initialize iinvocation instance 
			// with ordinary arguments and in and out arguments

			Expression interceptors;

			// if (useSelector)
			{
				// TODO: Generate code that checks the return of selector
				// if no interceptors is returned, should we invoke the base.Method directly?
			}
			// else
			{
				interceptors = interceptorsField.ToExpression();
			}

			Expression typeTokenFieldExp = typeTokenField.ToExpression();
			Expression methodInfoTokenExp;

			if (method2TokenField.ContainsKey(method)) // Token is in the cache
			{
				methodInfoTokenExp = ((FieldReference) method2TokenField[method]).ToExpression();
			}
			else
			{
				// Not in the cache: generic method

				methodInfoTokenExp = new MethodTokenExpression(method.MakeGenericMethod(genericMethodArgs));
			}

			ConstructorInfo constructor = invocationImpl.Constructors[0].ConstructorBuilder;

			if (isGenericInvocationClass)
			{
				constructor = TypeBuilder.GetConstructor(iinvocation, invocationImpl.Constructors[0].ConstructorBuilder);
			}

			NewInstanceExpression newInvocImpl;

			if (version == ConstructorVersion.WithTargetMethod)
			{
				Expression methodOnTargetTokenExp;

				if (method2TokenField.ContainsKey(methodOnTarget)) // Token is in the cache
				{
					methodOnTargetTokenExp = ((FieldReference) method2TokenField[methodOnTarget]).ToExpression();
				}
				else
				{
					// Not in the cache: generic method

					methodOnTargetTokenExp = new MethodTokenExpression(methodOnTarget.MakeGenericMethod(genericMethodArgs));
				}

				newInvocImpl =
					new NewInstanceExpression(constructor,
					                          targetRef.ToExpression(),
					                          interceptors,
					                          typeTokenFieldExp,
					                          methodOnTargetTokenExp,
					                          methodInfoTokenExp,
					                          new ReferencesToObjectArrayExpression(dereferencedArguments),
					                          SelfReference.Self.ToExpression());
			}
			else
			{
				newInvocImpl =
					new NewInstanceExpression(constructor,
					                          targetRef.ToExpression(),
					                          interceptors,
					                          typeTokenFieldExp,
					                          methodInfoTokenExp,
					                          new ReferencesToObjectArrayExpression(dereferencedArguments),
					                          SelfReference.Self.ToExpression());
			}

			methodEmitter.CodeBuilder.AddStatement(new AssignStatement(invocationImplLocal, newInvocImpl));

			if (method.ContainsGenericParameters)
			{
				EmitLoadGenricMethodArguments(methodEmitter, method.MakeGenericMethod(genericMethodArgs), invocationImplLocal);
			}

			methodEmitter.CodeBuilder.AddStatement(
				new ExpressionStatement(new MethodInvocationExpression(invocationImplLocal, Constants.AbstractInvocationProceed)));

			CopyOutAndRefParameters(dereferencedArguments, invocationImplLocal, method, methodEmitter);

			if (method.ReturnType != typeof(void))
			{
				// Emit code to return with cast from ReturnValue
				MethodInvocationExpression getRetVal =
					new MethodInvocationExpression(invocationImplLocal, typeof(AbstractInvocation).GetMethod("get_ReturnValue"));

				methodEmitter.CodeBuilder.AddStatement(
					new ReturnStatement(new ConvertExpression(methodEmitter.ReturnType, getRetVal)));
			}
			else
			{
				methodEmitter.CodeBuilder.AddStatement(new ReturnStatement());
			}

			return methodEmitter;
		}
コード例 #9
0
        protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
		                                                        ProxyGenerationOptions options, INamingScope namingScope)
        {
            var invocationType = invocation;

            Trace.Assert(MethodToOverride.IsGenericMethod == invocationType.IsGenericTypeDefinition());
            var genericArguments = TypeExtender.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 dereferencedArguments = IndirectReference.WrapIfByRef(emitter.Arguments);
            var hasByRefArguments = HasByRefArguments(emitter.Arguments);

            var arguments = GetCtorArguments(@class, namingScope, proxiedMethodTokenExpression,
                                             dereferencedArguments);
            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

                // @mbrit - 2012-05-31 - see the note associated with the GetReturnValueForWinRt declaration
                // for more information on this...

                var useWinRtGenericHandler = false;
            #if NETFX_CORE
                if (emitter.ReturnType == typeof(int) || emitter.ReturnType == typeof(bool))
                    useWinRtGenericHandler = true;
            #endif
                if(!(useWinRtGenericHandler))
                {
                    var getRetVal = new MethodInvocationExpression(invocationLocal, InvocationMethods.GetReturnValue);
                    emitter.CodeBuilder.AddStatement(new ReturnStatement(new ConvertExpression(emitter.ReturnType, getRetVal)));
                }
                else
                {
            #if NETFX_CORE
                    var grvArgs = new Type[] { emitter.ReturnType };
                    var grvCall = InvocationMethods.GetReturnValueForWinRt.MakeGenericMethod(grvArgs);
                    var getRetVal = new MethodInvocationExpression(invocationLocal, grvCall);
                    emitter.CodeBuilder.AddStatement(new ReturnStatement(getRetVal));
            #endif
                }
            }
            else
            {
                emitter.CodeBuilder.AddStatement(new ReturnStatement());
            }

            return emitter;
        }
コード例 #10
0
        protected void GenerateSerializationConstructor(ClassEmitter emitter, FieldReference interceptorField,
		                                                bool delegateToBaseGetObjectData)
        {
            ArgumentReference arg1 = new ArgumentReference(typeof(SerializationInfo));
            ArgumentReference arg2 = new ArgumentReference(typeof(StreamingContext));

            ConstructorEmitter constr = emitter.CreateConstructor(arg1, arg2);

            constr.CodeBuilder.AddStatement(
                new ConstructorInvocationStatement(serializationConstructor,
                                                   arg1.ToExpression(), arg2.ToExpression()));

            Type[] object_arg = new Type[] {typeof(String), typeof(Type)};
            MethodInfo getValueMethod = typeof(SerializationInfo).GetMethod("GetValue", object_arg);

            MethodInvocationExpression getInterceptorInvocation =
                new MethodInvocationExpression(arg1, getValueMethod,
                                               new ConstReference("__interceptors").ToExpression(),
                                               new TypeTokenExpression(typeof(IInterceptor[])));

            constr.CodeBuilder.AddStatement(new AssignStatement(
                                                interceptorField,
                                                new ConvertExpression(typeof(IInterceptor[]), typeof(object),
                                                                      getInterceptorInvocation)));

            constr.CodeBuilder.AddStatement(new ReturnStatement());
        }
コード例 #11
0
		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();
		}
コード例 #12
0
		protected MethodEmitter ImplementProxiedMethod(
			Type targetType,
			MethodEmitter methodEmitter,
			MethodInfo method,
			ClassEmitter emitter,
			NestedClassEmitter invocationImpl,
			FieldReference interceptorsField,
			Reference targetRef,
			ConstructorVersion version,
			MethodInfo methodOnTarget)
		{
			CheckNotGenericTypeDefinition(targetType, "targetType");

			methodEmitter.CopyParametersAndReturnTypeFrom(method, emitter);

			TypeReference[] dereferencedArguments = IndirectReference.WrapIfByRef(methodEmitter.Arguments);

			Type iinvocation = invocationImpl.TypeBuilder;

			Trace.Assert(method.IsGenericMethod == iinvocation.IsGenericTypeDefinition);
			bool isGenericInvocationClass = false;
			Type[] genericMethodArgs = new Type[0];
			if (method.IsGenericMethod)
			{
				// bind generic method arguments to invocation's type arguments
				genericMethodArgs = methodEmitter.MethodBuilder.GetGenericArguments();
				iinvocation = iinvocation.MakeGenericType(genericMethodArgs);
				isGenericInvocationClass = true;
			}

			Expression typeTokenFieldExp = typeTokenField.ToExpression();
			Expression methodInfoTokenExp;

			string tokenFieldName;
			if (method2TokenField.ContainsKey(method)) // Token is in the cache
			{
				FieldReference methodTokenField = method2TokenField[method];
				tokenFieldName = methodTokenField.Reference.Name;
				methodInfoTokenExp = methodTokenField.ToExpression();
			}
			else
			{
				// Not in the cache: generic method
				MethodInfo genericMethod = method.MakeGenericMethod(genericMethodArgs);

				// Need random suffix added to the name, so that we don't end up with duplicate field names for
				// methods with the same name, but different generic parameters
				tokenFieldName = string.Format("{0}_{1}_{2}", genericMethod.Name, genericMethodArgs.Length,
				                               Guid.NewGuid().ToString("N"));
				methodInfoTokenExp = new MethodTokenExpression(genericMethod);
			}

			LocalReference invocationImplLocal = methodEmitter.CodeBuilder.DeclareLocal(iinvocation);

			// TODO: Initialize iinvocation instance with ordinary arguments and in and out arguments

			Expression interceptors = interceptorsField.ToExpression();

			// Create the field to store the selected interceptors for this method if an InterceptorSelector is specified
			FieldReference methodInterceptors = null;
			if (proxyGenerationOptions.Selector != null)
			{
				// If no interceptors are returned, should we invoke the base.Method directly? Looks like we should not.
				methodInterceptors = emitter.CreateField(string.Format("{0}_interceptors", tokenFieldName), typeof(IInterceptor[]));
			}

			ConstructorInfo constructor = invocationImpl.Constructors[0].ConstructorBuilder;
			if (isGenericInvocationClass)
			{
				constructor = TypeBuilder.GetConstructor(iinvocation, constructor);
			}

			NewInstanceExpression newInvocImpl;

			if (version == ConstructorVersion.WithTargetMethod)
			{
				Expression methodOnTargetTokenExp;

				if (method2TokenField.ContainsKey(methodOnTarget)) // Token is in the cache
				{
					methodOnTargetTokenExp = method2TokenField[methodOnTarget].ToExpression();
				}
				else
				{
					// Not in the cache: generic method

					methodOnTargetTokenExp = new MethodTokenExpression(methodOnTarget.MakeGenericMethod(genericMethodArgs));
				}
				if (methodInterceptors == null)
				{
					newInvocImpl = //actual contructor call
						new NewInstanceExpression(constructor,
						                          targetRef.ToExpression(),
						                          interceptors,
						                          typeTokenFieldExp,
						                          methodOnTargetTokenExp,
						                          methodInfoTokenExp,
						                          new ReferencesToObjectArrayExpression(dereferencedArguments),
						                          SelfReference.Self.ToExpression());
				}
				else
				{
					MethodInvocationExpression methodInvocationExpression =
						new MethodInvocationExpression(proxyGenerationOptionsField, proxyGenerationOptions_Selector);
					methodInvocationExpression.VirtualCall = true;

					newInvocImpl = //actual contructor call
						new NewInstanceExpression(constructor,
						                          targetRef.ToExpression(),
						                          interceptors,
						                          typeTokenFieldExp,
						                          methodOnTargetTokenExp,
						                          methodInfoTokenExp,
						                          new ReferencesToObjectArrayExpression(dereferencedArguments),
						                          SelfReference.Self.ToExpression(),
						                          methodInvocationExpression,
						                          new AddressOfReferenceExpression(methodInterceptors));
				}
			}
			else
			{
				if (methodInterceptors == null)
				{
					newInvocImpl =
						new NewInstanceExpression(constructor,
						                          targetRef.ToExpression(),
						                          interceptors,
						                          typeTokenFieldExp,
						                          methodInfoTokenExp,
						                          new ReferencesToObjectArrayExpression(dereferencedArguments),
						                          SelfReference.Self.ToExpression());
				}
				else
				{
					MethodInvocationExpression methodInvocationExpression =
						new MethodInvocationExpression(proxyGenerationOptionsField, proxyGenerationOptions_Selector);
					methodInvocationExpression.VirtualCall = true;

					newInvocImpl =
						new NewInstanceExpression(constructor,
						                          targetRef.ToExpression(),
						                          interceptors,
						                          typeTokenFieldExp,
						                          methodInfoTokenExp,
						                          new ReferencesToObjectArrayExpression(dereferencedArguments),
						                          SelfReference.Self.ToExpression(),
						                          methodInvocationExpression,
						                          new AddressOfReferenceExpression(methodInterceptors));
				}
			}

			methodEmitter.CodeBuilder.AddStatement(new AssignStatement(invocationImplLocal, newInvocImpl));

			if (method.ContainsGenericParameters)
			{
				EmitLoadGenricMethodArguments(methodEmitter, method.MakeGenericMethod(genericMethodArgs), invocationImplLocal);
			}

			methodEmitter.CodeBuilder.AddStatement(
				new ExpressionStatement(new MethodInvocationExpression(invocationImplLocal, Constants.AbstractInvocationProceed)));

			CopyOutAndRefParameters(dereferencedArguments, invocationImplLocal, method, methodEmitter);

			if (method.ReturnType != typeof(void))
			{
				// Emit code to return with cast from ReturnValue
				MethodInvocationExpression getRetVal =
					new MethodInvocationExpression(invocationImplLocal, typeof(AbstractInvocation).GetMethod("get_ReturnValue"));

				methodEmitter.CodeBuilder.AddStatement(
					new ReturnStatement(new ConvertExpression(methodEmitter.ReturnType, getRetVal)));
			}
			else
			{
				methodEmitter.CodeBuilder.AddStatement(new ReturnStatement());
			}

			return methodEmitter;
		}