Exemplo n.º 1
0
 public EasyMethod CreateRemoveOnMethod(MethodAttributes atts, params Type[] parameters)
 {
     if (this.m_removeOnMethod == null)
     {
         this.m_removeOnMethod = new EasyMethod(this.m_maintype, "remove_" + this.Name, atts, new ReturnReferenceExpression(typeof(void)), ArgumentsUtil.ConvertToArgumentReference(parameters));
     }
     return(this.m_removeOnMethod);
 }
Exemplo n.º 2
0
 public EasyMethod CreateSetMethod(MethodAttributes attrs, params Type[] parameters)
 {
     if (this._setMethod == null)
     {
         this._setMethod = new EasyMethod(this._maintype, "set_" + this._builder.Name, attrs, new ReturnReferenceExpression(typeof(void)), ArgumentsUtil.ConvertToArgumentReference(parameters));
     }
     return(this._setMethod);
 }
Exemplo n.º 3
0
		public EasyMethod CreateRemoveOnMethod(MethodAttributes atts, params Type[] parameters)
		{
			if (m_removeOnMethod == null)
			{
				m_removeOnMethod =
					new EasyMethod(m_maintype, "remove_" + Name, atts, new ReturnReferenceExpression(typeof(void)), ArgumentsUtil.ConvertToArgumentReference(parameters));
			}

			return m_removeOnMethod;
		}
Exemplo n.º 4
0
        public EasyMethod CreateAddOnMethod(MethodAttributes atts, params Type[] parameters)
        {
            if (m_addOnMethod == null)
            {
                m_addOnMethod =
                    new EasyMethod(m_maintype, "add_" + Name, atts, new ReturnReferenceExpression(typeof(void)), ArgumentsUtil.ConvertToArgumentReference(parameters));
            }

            return(m_addOnMethod);
        }
Exemplo n.º 5
0
        public EasyMethod CreateGetMethod(MethodAttributes attrs, params Type[] parameters)
        {
            if (_getMethod != null)
            {
                return(_getMethod);
            }

            _getMethod = new EasyMethod(_maintype, "get_" + _builder.Name,
                                        attrs,
                                        new ReturnReferenceExpression(ReturnType),
                                        ArgumentsUtil.ConvertToArgumentReference(parameters));

            return(_getMethod);
        }
Exemplo n.º 6
0
		public EasyMethod CreateSetMethod(MethodAttributes attrs, params Type[] parameters)
		{
			if (_setMethod != null)
			{
				return _setMethod;
			}

			_setMethod = new EasyMethod(_maintype, "set_" + _builder.Name,
			                            attrs,
			                            new ReturnReferenceExpression(typeof(void)),
			                            ArgumentsUtil.ConvertToArgumentReference(parameters));

			return _setMethod;
		}
Exemplo n.º 7
0
        private void GenerateCall()
        {
            ArgumentReference arrayReference = new ArgumentReference(typeof(object[]));

            this._callmethod = base.CreateMethod("Call", new ReturnReferenceExpression(typeof(object)), new ArgumentReference[] { arrayReference });
            TypeReference[]  referenceArray  = IndirectReference.WrapIfByRef(this._args);
            LocalReference[] referenceArray2 = new LocalReference[this._args.Length];
            Expression[]     args            = new Expression[this._args.Length];
            for (int i = 0; i < this._args.Length; i++)
            {
                if (this._args[i].Type.IsByRef)
                {
                    referenceArray2[i] = this._callmethod.CodeBuilder.DeclareLocal(referenceArray[i].Type);
                    this._callmethod.CodeBuilder.AddStatement(new AssignStatement(referenceArray2[i], new ConvertExpression(referenceArray[i].Type, new LoadRefArrayElementExpression(i, arrayReference))));
                    args[i] = referenceArray2[i].ToAddressOfExpression();
                }
                else
                {
                    args[i] = new ConvertExpression(referenceArray[i].Type, new LoadRefArrayElementExpression(i, arrayReference));
                }
            }
            MethodInvocationExpression expression = new MethodInvocationExpression(this._invokeMethod, args);
            Expression instance = null;

            if (this._returnType.Type == typeof(void))
            {
                this._callmethod.CodeBuilder.AddStatement(new ExpressionStatement(expression));
                instance = NullExpression.Instance;
            }
            else
            {
                LocalReference target = this._callmethod.CodeBuilder.DeclareLocal(typeof(object));
                this._callmethod.CodeBuilder.AddStatement(new AssignStatement(target, new ConvertExpression(typeof(object), this._returnType.Type, expression)));
                instance = target.ToExpression();
            }
            for (int j = 0; j < this._args.Length; j++)
            {
                if (this._args[j].Type.IsByRef)
                {
                    this._callmethod.CodeBuilder.AddStatement(new AssignArrayStatement(arrayReference, j, new ConvertExpression(typeof(object), referenceArray[j].Type, referenceArray2[j].ToExpression())));
                }
            }
            this._callmethod.CodeBuilder.AddStatement(new ReturnStatement(instance));
        }
Exemplo n.º 8
0
		public void Add(EasyMethod method)
		{
			InnerList.Add( method );
		}
Exemplo n.º 9
0
        private void GenerateCall()
        {
            ArgumentReference arg = new ArgumentReference( typeof(object[]) );
            _callmethod = CreateMethod( "Call",
                new ReturnReferenceExpression(typeof(object)), arg );

            // LocalReference localRef = method.CodeBuilder.DeclareLocal( typeof(object) );

            TypeReference[] dereferencedArguments = IndirectReference.WrapIfByRef(_args);
            LocalReference[] localCopies = new LocalReference[_args.Length];
            Expression[] invocationArguments = new Expression[_args.Length];

            // Load arguments from the object array.
            for (int i = 0; i < _args.Length; i++)
            {
                if (_args[i].Type.IsByRef)
                {
                    localCopies[i] = _callmethod.CodeBuilder.DeclareLocal(dereferencedArguments[i].Type);

                    _callmethod.CodeBuilder.AddStatement(new AssignStatement(localCopies[i],
                        new ConvertExpression(dereferencedArguments[i].Type,
                            new LoadRefArrayElementExpression(i, arg))));

                    invocationArguments[i] = localCopies[i].ToAddressOfExpression();
                }
                else
                {
                    invocationArguments[i] = new ConvertExpression(dereferencedArguments[i].Type,
                        new LoadRefArrayElementExpression(i, arg));
                }
            }

            // Invoke the method.
            MethodInvocationExpression methodInv = new MethodInvocationExpression(
                _invokeMethod,
                invocationArguments );

            Expression result = null;
            if (_returnType.Type == typeof(void))
            {
                _callmethod.CodeBuilder.AddStatement(new ExpressionStatement(methodInv));
                result = NullExpression.Instance;
            }
            else
            {
                LocalReference resultLocal = _callmethod.CodeBuilder.DeclareLocal(typeof(object));

                _callmethod.CodeBuilder.AddStatement(new AssignStatement(resultLocal,
                    new ConvertExpression(typeof(object), _returnType.Type, methodInv)));

                result = resultLocal.ToExpression();
            }

            // Save ByRef arguments into the object array.
            for (int i = 0; i < _args.Length; i++)
            {
                if (_args[i].Type.IsByRef)
                {
                    _callmethod.CodeBuilder.AddStatement(new AssignArrayStatement(arg, i,
                        new ConvertExpression(typeof(object), dereferencedArguments[i].Type,
                            localCopies[i].ToExpression())));
                }
            }

            // Return.
            _callmethod.CodeBuilder.AddStatement( new ReturnStatement( result ) );
        }
Exemplo n.º 10
0
 public void Add(EasyMethod method)
 {
     InnerList.Add(method);
 }
Exemplo n.º 11
0
		public EasyMethod CreateMethod( String name, MethodAttributes attrs, ReturnReferenceExpression returnType, params Type[] args)
		{
			EasyMethod member = new EasyMethod( this, name, attrs, returnType, ArgumentsUtil.ConvertToArgumentReference(args) );
			_methods.Add(member);
			return member;
		}
Exemplo n.º 12
0
		public EasyMethod CreateMethod( String name, ReturnReferenceExpression returnType, MethodAttributes attributes, params ArgumentReference[] arguments )
		{
			EasyMethod member = new EasyMethod( this, name, attributes, returnType, arguments );
			_methods.Add(member);
			return member;
		}
Exemplo n.º 13
0
        /// <summary>
        /// Writes the method implementation. This 
        /// method generates the IL code for property get/set method and
        /// ordinary methods.
        /// </summary>
        /// <param name="method">The method to implement.</param>
        /// <param name="builder"><see cref="EasyMethod"/> being constructed.</param>
        protected virtual void WriteInterceptorInvocationMethod(MethodInfo method, EasyMethod builder)
        {
            ArgumentReference[] arguments = builder.Arguments;
            TypeReference[] dereferencedArguments = IndirectReference.WrapIfByRef(builder.Arguments);

            LocalReference local_inv = builder.CodeBuilder.DeclareLocal(Context.Invocation);

            EasyCallable callable = _method2Delegate[method] as EasyCallable;
            FieldReference fieldDelegate = ObtainCallableFieldBuilderDelegate(callable);

            builder.CodeBuilder.AddStatement(
                new AssignStatement(local_inv,
                                    new MethodInvocationExpression(_method2Invocation,
                                                                   fieldDelegate.ToExpression(),
                                                                   new MethodTokenExpression(GetCorrectMethod(method)),
                                                                   GetPseudoInvocationTarget(method))));

            LocalReference ret_local = builder.CodeBuilder.DeclareLocal(typeof(object));
            LocalReference args_local = builder.CodeBuilder.DeclareLocal(typeof(object[]));

            // Store arguments into an object array.
            builder.CodeBuilder.AddStatement(
                new AssignStatement(args_local,
                                    new ReferencesToObjectArrayExpression(dereferencedArguments)));

            // Invoke the interceptor.
            builder.CodeBuilder.AddStatement(
                new AssignStatement(ret_local,
                                    new VirtualMethodInvocationExpression(InterceptorField,
                                                                          Context.Interceptor.GetMethod("Intercept"),
                                                                          local_inv.ToExpression(),
                                                                          args_local.ToExpression())));

            // Load possibly modified ByRef arguments from the array.
            for(int i = 0; i < arguments.Length; i++)
            {
                if (arguments[i].Type.IsByRef)
                {
                    builder.CodeBuilder.AddStatement(
                        new AssignStatement(dereferencedArguments[i],
                                            new ConvertExpression(dereferencedArguments[i].Type,
                                                                  new LoadRefArrayElementExpression(i, args_local))));
                }
            }

            if (builder.ReturnType == typeof(void))
            {
                builder.CodeBuilder.AddStatement(new ReturnStatement());
            }
            else
            {
                builder.CodeBuilder.AddStatement(new ReturnStatement(
                                                 	new ConvertExpression(builder.ReturnType, ret_local.ToExpression())));
            }
        }
Exemplo n.º 14
0
        protected virtual void ImplementCacheInvocationCache()
        {
            MethodInfo get_ItemMethod = typeof(HybridDictionary).GetMethod("get_Item", new Type[] {typeof(object)});
            MethodInfo set_ItemMethod = typeof(HybridDictionary).GetMethod("Add", new Type[] {typeof(object), typeof(object)});

            Type[] args = new Type[] {typeof(ICallable), typeof(MethodInfo)};
            Type[] invocation_const_args = new Type[] {typeof(ICallable), typeof(object), typeof(MethodInfo), typeof(object)};

            ArgumentReference arg1 = new ArgumentReference(typeof(ICallable));
            ArgumentReference arg2 = new ArgumentReference(typeof(MethodInfo));
            ArgumentReference arg3 = new ArgumentReference(typeof(object));

            _method2Invocation = MainTypeBuilder.CreateMethod("_Method2Invocation",
                                                              new ReturnReferenceExpression(Context.Invocation),
                                                              MethodAttributes.Family | MethodAttributes.HideBySig, arg1, arg2,
                                                              arg3);

            LocalReference invocation_local =
                _method2Invocation.CodeBuilder.DeclareLocal(Context.Invocation);

            LockBlockExpression block = new LockBlockExpression(SelfReference.Self);

            block.AddStatement(new AssignStatement(invocation_local,
                                                   new ConvertExpression(Context.Invocation,
                                                                         new VirtualMethodInvocationExpression(CacheField,
                                                                                                               get_ItemMethod,
                                                                                                               arg2.ToExpression()))));

            ConditionExpression cond1 = new ConditionExpression(OpCodes.Brfalse_S,
                                                                invocation_local.ToExpression());

            cond1.AddTrueStatement(new AssignStatement(
                                   	invocation_local,
                                   	new NewInstanceExpression(InvocationType.GetConstructor(invocation_const_args),
                                   	                          arg1.ToExpression(), SelfReference.Self.ToExpression(),
                                   	                          arg2.ToExpression(), arg3.ToExpression())));

            cond1.AddTrueStatement(new ExpressionStatement(
                                   	new VirtualMethodInvocationExpression(CacheField,
                                   	                                      set_ItemMethod, arg2.ToExpression(),
                                   	                                      invocation_local.ToExpression())));

            block.AddStatement(new ExpressionStatement(cond1));

            _method2Invocation.CodeBuilder.AddStatement(new ExpressionStatement(block));
            _method2Invocation.CodeBuilder.AddStatement(new ReturnStatement(invocation_local));
        }
Exemplo n.º 15
0
        private void GenerateCall()
        {
            ArgumentReference arg = new ArgumentReference(typeof(object[]));

            _callmethod = CreateMethod("Call",
                                       new ReturnReferenceExpression(typeof(object)), arg);

            // LocalReference localRef = method.CodeBuilder.DeclareLocal( typeof(object) );

            TypeReference[]  dereferencedArguments = IndirectReference.WrapIfByRef(_args);
            LocalReference[] localCopies           = new LocalReference[_args.Length];
            Expression[]     invocationArguments   = new Expression[_args.Length];

            // Load arguments from the object array.
            for (int i = 0; i < _args.Length; i++)
            {
                if (_args[i].Type.IsByRef)
                {
                    localCopies[i] = _callmethod.CodeBuilder.DeclareLocal(dereferencedArguments[i].Type);

                    _callmethod.CodeBuilder.AddStatement(new AssignStatement(localCopies[i],
                                                                             new ConvertExpression(dereferencedArguments[i].Type,
                                                                                                   new LoadRefArrayElementExpression(i, arg))));

                    invocationArguments[i] = localCopies[i].ToAddressOfExpression();
                }
                else
                {
                    invocationArguments[i] = new ConvertExpression(dereferencedArguments[i].Type,
                                                                   new LoadRefArrayElementExpression(i, arg));
                }
            }

            // Invoke the method.
            MethodInvocationExpression methodInv = new MethodInvocationExpression(
                _invokeMethod,
                invocationArguments);

            Expression result = null;

            if (_returnType.Type == typeof(void))
            {
                _callmethod.CodeBuilder.AddStatement(new ExpressionStatement(methodInv));
                result = NullExpression.Instance;
            }
            else
            {
                LocalReference resultLocal = _callmethod.CodeBuilder.DeclareLocal(typeof(object));

                _callmethod.CodeBuilder.AddStatement(new AssignStatement(resultLocal,
                                                                         new ConvertExpression(typeof(object), _returnType.Type, methodInv)));

                result = resultLocal.ToExpression();
            }

            // Save ByRef arguments into the object array.
            for (int i = 0; i < _args.Length; i++)
            {
                if (_args[i].Type.IsByRef)
                {
                    _callmethod.CodeBuilder.AddStatement(new AssignArrayStatement(arg, i,
                                                                                  new ConvertExpression(typeof(object), dereferencedArguments[i].Type,
                                                                                                        localCopies[i].ToExpression())));
                }
            }

            // Return.
            _callmethod.CodeBuilder.AddStatement(new ReturnStatement(result));
        }