CreateCallable() public method

public CreateCallable ( ReturnReferenceExpression returnType ) : EasyCallable
returnType Castle.DynamicProxy.Builder.CodeBuilder.SimpleAST.ReturnReferenceExpression
return EasyCallable
Esempio n. 1
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 );
		}