예제 #1
0
		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();
		}
예제 #2
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 );
		}
예제 #3
0
		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();
		}
예제 #4
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();
		}
예제 #5
0
		public void BlockWithLock()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

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

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

			LockBlockExpression block = new LockBlockExpression( SelfReference.Self );

			block.AddStatement( new AssignStatement(cachefield, 
				new NewInstanceExpression( typeof(ArrayList), new Type[0] ) ) );
			
			constructor.CodeBuilder.AddStatement( new ExpressionStatement(block) );
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

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

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

			MethodInfo method = instance.GetType().GetMethod("GetCache");
			Assert.IsNotNull( method.Invoke( instance, new object[0] ) );

			RunPEVerify();
		}
예제 #6
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();
		}