private void AddOverrider(MethodInfo method, Reference invocationHandler) { var overrider = _emitter.CreateMethod(method.Name, MethodAttributes.Public | MethodAttributes.Virtual, method); overrider.AddCustomAttribute(new CustomAttributeBuilder(s_attributeConstructor, new object[0])); Reference targetReference = GetTargetReference(); Expression overriddenMethodToken = new MethodTokenExpression(method); LocalReference argsLocal = CopyArgumentsToLocalVariable(overrider); Expression baseMethodInvoker = GetBaseInvokerExpression(method); TypeReference handlerReference = new TypeReferenceWrapper(invocationHandler, typeof(MethodInvocationHandler)); Expression[] handlerArgs = new Expression[] { targetReference.ToExpression(), overriddenMethodToken, argsLocal.ToExpression(), baseMethodInvoker }; Expression handlerInvocation = new VirtualMethodInvocationExpression(handlerReference, s_handlerInvokeMethod, handlerArgs); if (method.ReturnType != typeof(void)) { overrider.ImplementByReturning(new ConvertExpression(method.ReturnType, typeof(object), handlerInvocation)); } else { overrider.AddStatement(new ExpressionStatement(handlerInvocation)); overrider.AddStatement(new PopStatement()); overrider.ImplementByReturningVoid(); } }
private MethodInfo CreateBaseCallStub(MethodInfo methodToBeStubbed) { var stubMethod = _emitter.CreateMethod( methodToBeStubbed.Name + "__stub", MethodAttributes.Private, typeof(object), new[] { typeof(object[]) }); PropertyReference baseReference = GetNextReference(); ParameterInfo[] stubbedMethodSignature = methodToBeStubbed.GetParameters(); Expression[] baseCallArgs = new Expression[stubbedMethodSignature.Length]; for (int i = 0; i < baseCallArgs.Length; ++i) { baseCallArgs[i] = new ConvertExpression(stubbedMethodSignature[i].ParameterType, typeof(object), new LoadArrayElementExpression(i, stubMethod.ArgumentReferences[0], typeof(object))); } MethodInfo methodOnBaseInterface = _baseCallInterface.GetBaseCallMethod(methodToBeStubbed); Expression baseMethodCall = new VirtualMethodInvocationExpression(baseReference, methodOnBaseInterface, baseCallArgs); if (methodToBeStubbed.ReturnType != typeof(void)) { stubMethod.ImplementByReturning(baseMethodCall); } else { stubMethod.AddStatement(new ExpressionStatement(baseMethodCall)); stubMethod.ImplementByReturning(NullExpression.Instance); } return(stubMethod.MethodBuilder); }
protected void GenerateSerializationConstructor() { ArgumentReference arg1 = new ArgumentReference(typeof(SerializationInfo)); ArgumentReference arg2 = new ArgumentReference(typeof(StreamingContext)); EasyConstructor constr = MainTypeBuilder.CreateConstructor(arg1, arg2); constr.CodeBuilder.AddStatement(new ExpressionStatement( new ConstructorInvocationExpression(_serializationConstructor, arg1.ToExpression(), arg2.ToExpression()))); Type[] object_arg = new Type[] { typeof(String), typeof(Type) }; MethodInfo getValueMethod = typeof(SerializationInfo).GetMethod("GetValue", object_arg); VirtualMethodInvocationExpression getInterceptorInvocation = new VirtualMethodInvocationExpression(arg1, getValueMethod, new FixedReference("__interceptor").ToExpression(), new TypeTokenExpression(Context.Interceptor)); VirtualMethodInvocationExpression getMixinsInvocation = new VirtualMethodInvocationExpression(arg1, getValueMethod, new FixedReference("__mixins").ToExpression(), new TypeTokenExpression(typeof(object[]))); constr.CodeBuilder.AddStatement(new AssignStatement( InterceptorField, getInterceptorInvocation)); constr.CodeBuilder.AddStatement(new AssignStatement( CacheField, new NewInstanceExpression( typeof(HybridDictionary).GetConstructor(new Type[0])))); constr.CodeBuilder.AddStatement(new AssignStatement( MixinField, getMixinsInvocation)); // Initialize the delegate fields foreach (CallableField field in _cachedFields) { field.WriteInitialization(constr.CodeBuilder, SelfReference.Self, MixinField); } constr.CodeBuilder.AddStatement(new ReturnStatement()); }
protected void GenerateSerializationConstructor() { ArgumentReference owner = new ArgumentReference(typeof(SerializationInfo)); ArgumentReference reference2 = new ArgumentReference(typeof(StreamingContext)); EasyConstructor constructor = base.MainTypeBuilder.CreateConstructor(new ArgumentReference[] { owner, reference2 }); constructor.CodeBuilder.AddStatement(new ExpressionStatement(new ConstructorInvocationExpression(this._serializationConstructor, new Expression[] { owner.ToExpression(), reference2.ToExpression() }))); Type[] types = new Type[] { typeof(string), typeof(Type) }; MethodInfo method = typeof(SerializationInfo).GetMethod("GetValue", types); VirtualMethodInvocationExpression expression = new VirtualMethodInvocationExpression(owner, method, new Expression[] { new FixedReference("__interceptor").ToExpression(), new TypeTokenExpression(base.Context.Interceptor) }); VirtualMethodInvocationExpression expression2 = new VirtualMethodInvocationExpression(owner, method, new Expression[] { new FixedReference("__mixins").ToExpression(), new TypeTokenExpression(typeof(object[])) }); constructor.CodeBuilder.AddStatement(new AssignStatement(base.InterceptorField, expression)); constructor.CodeBuilder.AddStatement(new AssignStatement(base.CacheField, new NewInstanceExpression(typeof(HybridDictionary).GetConstructor(new Type[0]), new Expression[0]))); constructor.CodeBuilder.AddStatement(new AssignStatement(base.MixinField, expression2)); foreach (CallableField field in base._cachedFields) { field.WriteInitialization(constructor.CodeBuilder, SelfReference.Self, base.MixinField); } constructor.CodeBuilder.AddStatement(new ReturnStatement()); }