Example #1
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() );
		}
Example #2
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));
        }
Example #3
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() ) ) );
			}
		}
		/// <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;
		}
Example #5
0
		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 );
		}
Example #6
0
		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();
		}
Example #7
0
		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();
		}
Example #8
0
		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();
		}