Inheritance: Castle.DynamicProxy.Builder.CodeBuilder.SimpleAST.TypeReference
Exemple #1
0
		public static void InitializeArgumentsByPosition(ArgumentReference[] args)
		{
			for(int i=0; i < args.Length; ++i)
			{
				args[i].Position = i+1;
			}
		}
Exemple #2
0
		/// <summary>
		/// Generates one public constructor receiving 
		/// the <see cref="IInterceptor"/> instance and instantiating a hashtable
		/// </summary>
		protected virtual EasyConstructor GenerateConstructor( ConstructorInfo baseConstructor )
		{
			ArrayList arguments = new ArrayList();

			ArgumentReference arg1 = new ArgumentReference( Context.Interceptor );
			ArgumentReference arg2 = new ArgumentReference( typeof(object[]) );

			arguments.Add( arg1 );

			ParameterInfo[] parameters = baseConstructor.GetParameters();

			if (Context.HasMixins)
			{
				arguments.Add( arg2 );
			}

			ArgumentReference[] originalArguments = 
				ArgumentsUtil.ConvertToArgumentReference(parameters);

			arguments.AddRange(originalArguments);

			EasyConstructor constructor = MainTypeBuilder.CreateConstructor( 
				(ArgumentReference[]) arguments.ToArray( typeof(ArgumentReference) ) );

			GenerateConstructorCode(constructor.CodeBuilder, arg1, SelfReference.Self, arg2);

			constructor.CodeBuilder.InvokeBaseConstructor( baseConstructor, originalArguments );

			constructor.CodeBuilder.AddStatement( new ReturnStatement() );
			
			return constructor;
		}
Exemple #3
0
		public static Expression[] ConvertArgumentReferenceToExpression(ArgumentReference[] args)
		{
			Expression[] expressions = new Expression[args.Length];
			
			for(int i=0; i < args.Length; ++i)
			{
				expressions[i] = args[i].ToExpression();
			}

			return expressions;
		}
		public EasyRuntimeMethod(AbstractEasyType maintype, string name, 
			ReturnReferenceExpression returnRef, ArgumentReference[] arguments)
		{
			MethodAttributes atts = MethodAttributes.HideBySig|MethodAttributes.Public|MethodAttributes.Virtual;
			Type[] args = ArgumentsUtil.InitializeAndConvert( arguments );

			_builder = maintype.TypeBuilder.DefineMethod(
				name, atts, returnRef.Type, args);
			_builder.SetImplementationFlags(
				MethodImplAttributes.Runtime|MethodImplAttributes.Managed);
		}
Exemple #5
0
		public static ArgumentReference[] ConvertToArgumentReference(ParameterInfo[] args)
		{
			ArgumentReference[] arguments = new ArgumentReference[args.Length];
			
			for(int i=0; i < args.Length; ++i)
			{
				arguments[i] = new ArgumentReference( args[i].ParameterType );
			}

			return arguments;
		}
Exemple #6
0
		public static Type[] InitializeAndConvert(ArgumentReference[] args)
		{
			Type[] types = new Type[args.Length];

			for(int i=0; i < args.Length; ++i)
			{
				args[i].Position = i+1;
				types[i] = args[i].Type;
			}

			return types;
		}
		public void Conditionals()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference cachefield = typebuilder.CreateField( "cache", typeof(IDictionary) );

			ArgumentReference arg = new ArgumentReference( typeof(bool) );

			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();
			
			ConditionExpression exp = new ConditionExpression(OpCodes.Brtrue_S, arg.ToExpression());
			exp.AddTrueStatement( new AssignStatement(cachefield, 
				new NewInstanceExpression( typeof(HybridDictionary), new Type[0] ) ) );
			exp.AddFalseStatement( new AssignStatement(cachefield, 
				new NewInstanceExpression( typeof(Hashtable), new Type[0] ) ) );
			
			constructor.CodeBuilder.AddStatement( new ExpressionStatement(exp) );
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(IDictionary));
			EasyMethod getCache = typebuilder.CreateMethod( "GetCache", ret );
			getCache.CodeBuilder.AddStatement( new ReturnStatement( cachefield ) );

			Type newType = typebuilder.BuildType();
			object instance = Activator.CreateInstance( newType, new object[] { true } );
			MethodInfo method = instance.GetType().GetMethod("GetCache");
			object dic = method.Invoke( instance, new object[0] );
			Assert.IsTrue( dic is HybridDictionary );

			instance = Activator.CreateInstance( newType, new object[] { false } );
			dic = method.Invoke( instance, new object[0] );
			Assert.IsTrue( dic is Hashtable );

			RunPEVerify();
		}
Exemple #8
0
		protected override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, 
		                                               ArgumentReference arg1, ArgumentReference arg2)
		{
			Type[] key_and_object = new Type[] {typeof (String), typeof (Object)};
			Type[] key_and_bool = new Type[] {typeof (String), typeof (bool)};
			MethodInfo addValueMethod = typeof (SerializationInfo).GetMethod("AddValue", key_and_object);
			MethodInfo addValueBoolMethod = typeof (SerializationInfo).GetMethod("AddValue", key_and_bool);

			codebuilder.AddStatement( new ExpressionStatement(
				new VirtualMethodInvocationExpression(arg1, addValueBoolMethod, 
				new FixedReference("__delegateToBase").ToExpression(), 
				new FixedReference( _delegateToBaseGetObjectData ? 1 : 0 ).ToExpression() ) ) );

			if (_delegateToBaseGetObjectData)
			{
				MethodInfo baseGetObjectData = _baseType.GetMethod("GetObjectData", 
					new Type[] { typeof(SerializationInfo), typeof(StreamingContext) });

				codebuilder.AddStatement( new ExpressionStatement(
					new MethodInvocationExpression( baseGetObjectData, 
						arg1.ToExpression(), arg2.ToExpression() )) );
			}
			else
			{
				LocalReference members_ref = codebuilder.DeclareLocal( typeof(MemberInfo[]) );
				LocalReference data_ref = codebuilder.DeclareLocal( typeof(object[]) );

				MethodInfo getSerMembers = typeof(FormatterServices).GetMethod("GetSerializableMembers", 
					new Type[] { typeof(Type) });
				MethodInfo getObjData = typeof(FormatterServices).GetMethod("GetObjectData", 
					new Type[] { typeof(object), typeof(MemberInfo[]) });
				
				codebuilder.AddStatement( new AssignStatement( members_ref,
					new MethodInvocationExpression( null, getSerMembers, 
					new TypeTokenExpression( _baseType ) )) );
				
				codebuilder.AddStatement( new AssignStatement( data_ref, 
					new MethodInvocationExpression( null, getObjData, 
					SelfReference.Self.ToExpression(), members_ref.ToExpression() )) );

				codebuilder.AddStatement( new ExpressionStatement(
					new VirtualMethodInvocationExpression(arg1, addValueMethod, 
					new FixedReference("__data").ToExpression(), 
					data_ref.ToExpression() ) ) );
			}
		}
        protected virtual void ImplementGetObjectData(Type[] interfaces)
        {
            // To prevent re-implementation of this interface.
            _generated.Add(typeof(ISerializable));

            Type[] get_type_args = new Type[] {typeof(String), typeof(bool), typeof(bool)};
            Type[] key_and_object = new Type[] {typeof(String), typeof(Object)};
            MethodInfo addValueMethod = typeof(SerializationInfo).GetMethod("AddValue", key_and_object);

            ArgumentReference arg1 = new ArgumentReference(typeof(SerializationInfo));
            ArgumentReference arg2 = new ArgumentReference(typeof(StreamingContext));
            EasyMethod getObjectData = MainTypeBuilder.CreateMethod("GetObjectData",
                                                                    new ReturnReferenceExpression(typeof(void)), arg1, arg2);

            LocalReference typeLocal = getObjectData.CodeBuilder.DeclareLocal(typeof(Type));

            getObjectData.CodeBuilder.AddStatement(new AssignStatement(
                                                   	typeLocal,
                                                   	new MethodInvocationExpression(null,
                                                   	                               typeof(Type).GetMethod("GetType",
                                                   	                                                      get_type_args),
                                                   	                               new FixedReference(
                                                   	                               	Context.ProxyObjectReference.
                                                   	                               		AssemblyQualifiedName).ToExpression(),
                                                   	                               new FixedReference(1).ToExpression(),
                                                   	                               new FixedReference(0).ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                   	new VirtualMethodInvocationExpression(
                                                   		arg1, typeof(SerializationInfo).GetMethod("SetType"),
                                                   		typeLocal.ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                   	new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                   	                                      new FixedReference("__interceptor").
                                                   	                                      	ToExpression(),
                                                   	                                      InterceptorField.ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                   	new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                   	                                      new FixedReference("__mixins").
                                                   	                                      	ToExpression(),
                                                   	                                      MixinField.ToExpression())));

            LocalReference interfacesLocal =
                getObjectData.CodeBuilder.DeclareLocal(typeof(String[]));

            getObjectData.CodeBuilder.AddStatement(
                new AssignStatement(interfacesLocal,
                                    new NewArrayExpression(interfaces.Length, typeof(String))));

            for(int i = 0; i < interfaces.Length; i++)
            {
                getObjectData.CodeBuilder.AddStatement(new AssignArrayStatement(
                                                       	interfacesLocal, i,
                                                       	new FixedReference(interfaces[i].AssemblyQualifiedName).ToExpression()));
            }

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                   	new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                   	                                      new FixedReference("__interfaces").
                                                   	                                      	ToExpression(),
                                                   	                                      interfacesLocal.ToExpression())));

            getObjectData.CodeBuilder.AddStatement(new ExpressionStatement(
                                                   	new VirtualMethodInvocationExpression(arg1, addValueMethod,
                                                   	                                      new FixedReference("__baseType").
                                                   	                                      	ToExpression(),
                                                   	                                      new TypeTokenExpression(_baseType))));

            CustomizeGetObjectData(getObjectData.CodeBuilder, arg1, arg2);

            getObjectData.CodeBuilder.AddStatement(new ReturnStatement());
        }
        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));
        }
        protected virtual MethodInfo GenerateCallbackMethodIfNecessary(MethodInfo method, Reference invocationTarget)
        {
            if (Context.HasMixins && _interface2mixinIndex.Contains(method.DeclaringType))
            {
                return method;
            }

            String name = String.Format("callback__{0}", method.Name);

            ParameterInfo[] parameters = method.GetParameters();

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

            for(int i = 0; i < args.Length; i++)
            {
                args[i] = new ArgumentReference(parameters[i].ParameterType);
            }

            EasyMethod easymethod = MainTypeBuilder.CreateMethod(name,
                                                                 new ReturnReferenceExpression(method.ReturnType),
                                                                 MethodAttributes.HideBySig | MethodAttributes.Public, args);

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

            for(int i = 0; i < args.Length; i++)
            {
                exps[i] = args[i].ToExpression();
            }

            if (invocationTarget == null)
            {
                easymethod.CodeBuilder.AddStatement(
                    new ReturnStatement(
                        new MethodInvocationExpression(method, exps)));
            }
            else
            {
                easymethod.CodeBuilder.AddStatement(
                    new ReturnStatement(
                        new MethodInvocationExpression(invocationTarget, method, exps)));
            }

            return easymethod.MethodBuilder;
        }
 protected virtual void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference arg1,
     ArgumentReference arg2)
 {
 }
		public void EmptyMethodWithEnumTypeRefArg()
		{
			EasyType typebuilder = new EasyType(module, "mytype");

			SByteEnum refArgInst = SByteEnum.Two;
			Type refType = GetEnumRefType(ref refArgInst);

			Assert.IsTrue(refType.IsByRef);

			ArgumentReference refArg = new ArgumentReference(refType);
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));

			EasyMethod emptyMethod = typebuilder.CreateMethod("DoSomething", ret, refArg);

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull(newType);
			object instance = Activator.CreateInstance(newType);
			Assert.IsNotNull(instance);

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			method.Invoke(instance, new object[] { refArgInst });

			Assert.AreEqual(SByteEnum.Two, refArgInst, "Argument made round-trip successfully");

			RunPEVerify();
		}
		public void MethodInvokingMethod()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference rarg1 = new ArgumentReference(typeof(int));
			ArgumentReference rarg2 = new ArgumentReference(typeof(int));
			ReturnReferenceExpression rret = new ReturnReferenceExpression(typeof(int));
			EasyMethod realCalcMethod = typebuilder.CreateMethod( "RealCalc", rret, rarg1, rarg2 );
			realCalcMethod.CodeBuilder.AddStatement( 
				new ReturnStatement( 
					new BinaryExpression( BinaryExpression.Add, rarg1.ToExpression(), rarg2.ToExpression() ) ) );

			ArgumentReference arg1 = new ArgumentReference(typeof(int));
			ArgumentReference arg2 = new ArgumentReference(typeof(int));
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));
			EasyMethod calcMethod = typebuilder.CreateMethod( "Calc", ret, arg1, arg2 );
			calcMethod.CodeBuilder.AddStatement( 
				new ReturnStatement( 
					new MethodInvocationExpression( realCalcMethod, arg1.ToExpression(), arg2.ToExpression() ) ) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[0] );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("Calc");
			Assert.AreEqual( 2, method.Invoke( instance, new object[] { 1,1 } ) );
			Assert.AreEqual( 5, method.Invoke( instance, new object[] { 3,2 } ) );
			method = instance.GetType().GetMethod("RealCalc");
			Assert.AreEqual( 2, method.Invoke( instance, new object[] { 1,1 } ) );
			Assert.AreEqual( 5, method.Invoke( instance, new object[] { 3,2 } ) );

			RunPEVerify();
		}
Exemple #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 ) );
        }
		public void ArrayRefs()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference field1 = typebuilder.CreateField( "field1", typeof(object) );
			FieldReference field2 = typebuilder.CreateField( "field2", typeof(object) );

			ArgumentReference arg = new ArgumentReference( typeof(object[]) );

			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();

			constructor.CodeBuilder.AddStatement( new AssignStatement(field1, 
				new LoadRefArrayElementExpression( 0, arg ) ) );
			constructor.CodeBuilder.AddStatement( new AssignStatement(field2, 
				new LoadRefArrayElementExpression( 1, arg ) ) );
			
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(object));
			EasyMethod getField1 = typebuilder.CreateMethod( "GetField1", ret1 );
			getField1.CodeBuilder.AddStatement( new ReturnStatement( field1 ) );

			ReturnReferenceExpression ret2 = new ReturnReferenceExpression(typeof(object));
			EasyMethod getField2 = typebuilder.CreateMethod( "GetField2", ret2 );
			getField2.CodeBuilder.AddStatement( new ReturnStatement( field2 ) );

			Type newType = typebuilder.BuildType();
			object[] innerArgs = new object[] { "hammett", "verissimo" };
			object instance = Activator.CreateInstance( newType, new object[] { innerArgs } );

			MethodInfo method = instance.GetType().GetMethod("GetField1");
			object result = method.Invoke( instance, new object[0] );
			Assert.AreEqual( "hammett", result );
			
			method = instance.GetType().GetMethod("GetField2");
			result = method.Invoke( instance, new object[0] );
			Assert.AreEqual( "verissimo", result );

			RunPEVerify();
		}
		public void CreateSimpleTypeWithConstructor()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference arg1 = new ArgumentReference( typeof(String) );
			ArgumentReference arg2 = new ArgumentReference( typeof(int) );

			typebuilder.CreateConstructor( arg1, arg2 );
		
			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[] { "message", 10 } );
			Assert.IsNotNull( instance );

			RunPEVerify();
		}
Exemple #18
0
		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 override void CustomizeGetObjectData(AbstractCodeBuilder codebuilder, ArgumentReference arg1,
		                                               ArgumentReference arg2)
		{
			Type[] key_and_object = new Type[] {typeof(String), typeof(Object)};
			MethodInfo addValueMethod = typeof(SerializationInfo).GetMethod("AddValue", key_and_object);

			codebuilder.AddStatement(new ExpressionStatement(
			                         	new VirtualMethodInvocationExpression(arg1, addValueMethod,
			                         	                                      new FixedReference("__target").ToExpression(),
			                         	                                      _targetField.ToExpression())));
		}
Exemple #20
0
 private void GenerateConstructor()
 {
     ArgumentReference arg1 = new ArgumentReference( typeof(object) );
     ArgumentReference arg2 = new ArgumentReference( typeof(IntPtr) );
     _constructor = CreateRuntimeConstructor( arg1, arg2 );
 }
		public void CreateMoreComplexCallable()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference arg1 = new ArgumentReference( typeof(int) );
			ArgumentReference arg2 = new ArgumentReference( typeof(DateTime) );
			ArgumentReference arg3 = new ArgumentReference( typeof(object) );
			EasyCallable callable = typebuilder.CreateCallable( 
				new ReturnReferenceExpression(typeof(string)), 
				arg1, arg2, arg3 );

			FieldReference field1 = typebuilder.CreateField( "field1", callable.TypeBuilder );

			SimpleCallback sc = new SimpleCallback();

			ArgumentReference arg = new ArgumentReference( typeof(SimpleCallback) );
			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();

			constructor.CodeBuilder.AddStatement( new AssignStatement(field1, 
				new NewInstanceExpression( callable, 
				arg.ToExpression(), 
				new MethodPointerExpression( arg, typeof(SimpleCallback).GetMethod("RunAs") ) ) ) );
			
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			arg1 = new ArgumentReference( typeof(int) );
			arg2 = new ArgumentReference( typeof(DateTime) );
			arg3 = new ArgumentReference( typeof(object) );
			
			ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(string));
			
			EasyMethod getField1 = typebuilder.CreateMethod( "Exec", ret1, arg1, arg2, arg3 );
			getField1.CodeBuilder.AddStatement( 
				new ReturnStatement( 
					new ConvertExpression( typeof(String), 
						new MethodInvocationExpression( field1, 
							callable.Callmethod, 
							new ReferencesToObjectArrayExpression(arg1, arg2, arg3) ) ) ) );

			Type newType = typebuilder.BuildType();

			RunPEVerify();
			
			object instance = Activator.CreateInstance( newType, new object[] { sc } );

			MethodInfo method = instance.GetType().GetMethod("Exec");
			object result = method.Invoke( instance, new object[] { 1, DateTime.Now, "" } );
			Assert.AreEqual( "hello2", result );
		}
		/// <summary>
		/// Generates one public constructor receiving 
		/// the <see cref="IInterceptor"/> instance and instantiating a HybridCollection
		/// </summary>
		protected override EasyConstructor GenerateConstructor()
		{
			ArgumentReference arg1 = new ArgumentReference(Context.Interceptor);
			ArgumentReference arg2 = new ArgumentReference(typeof(object));
			ArgumentReference arg3 = new ArgumentReference(typeof(object[]));

			EasyConstructor constructor;

			if (Context.HasMixins)
			{
				constructor = MainTypeBuilder.CreateConstructor(arg1, arg2, arg3);
			}
			else
			{
				constructor = MainTypeBuilder.CreateConstructor(arg1, arg2);
			}

			GenerateConstructorCode(constructor.CodeBuilder, arg1, SelfReference.Self, arg3);

			constructor.CodeBuilder.InvokeBaseConstructor();

			constructor.CodeBuilder.AddStatement(new AssignStatement(
			                                     	_targetField, arg2.ToExpression()));

			constructor.CodeBuilder.AddStatement(new ReturnStatement());

			return constructor;
		}
		public void FieldsStoreAndLoad()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference field1 = typebuilder.CreateField( "field1", typeof(int) );
			FieldReference field2 = typebuilder.CreateField( "field2", typeof(string) );

			{
				ArgumentReference arg1 = new ArgumentReference(typeof(int));
				ArgumentReference arg2 = new ArgumentReference(typeof(string));

				EasyConstructor constr = typebuilder.CreateConstructor( arg1, arg2 );
				constr.CodeBuilder.InvokeBaseConstructor();
				constr.CodeBuilder.AddStatement( new AssignStatement( field1, arg1.ToExpression() ) );
				constr.CodeBuilder.AddStatement( new AssignStatement( field2, arg2.ToExpression() ) );
				constr.CodeBuilder.AddStatement( new ReturnStatement() );
			}

			{
				ReturnReferenceExpression ret1 = new ReturnReferenceExpression( typeof(int) );
				EasyMethod m1 = typebuilder.CreateMethod( "GetField1", ret1 );
				m1.CodeBuilder.AddStatement( new ReturnStatement( field1 ) );

				ReturnReferenceExpression ret2 = new ReturnReferenceExpression( typeof(string) );
				EasyMethod m2 = typebuilder.CreateMethod( "GetField2", ret2 );
				m2.CodeBuilder.AddStatement( new ReturnStatement( field2 ) );
			}

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[] { 10, "hello" } );
			Assert.IsNotNull( instance );

			MethodInfo method1 = instance.GetType().GetMethod("GetField1");
			MethodInfo method2 = instance.GetType().GetMethod("GetField2");
			Assert.AreEqual( 10, method1.Invoke( instance, new object[0] ));
			Assert.AreEqual( "hello", method2.Invoke( instance, new object[0] ));

			RunPEVerify();
		}