internal EasyMethod( AbstractEasyType maintype, String name, ReturnReferenceExpression returnRef, params ArgumentReference[] arguments ) : this(maintype, name, MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public, returnRef, arguments) { }
public EasyCallable CreateCallable(ReturnReferenceExpression returnType, params ArgumentReference[] args) { EasyCallable nested = new EasyCallable(this, IncrementAndGetCounterValue, returnType, args); _nested.Add(nested); return nested; }
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); }
public EasyNested(AbstractEasyType maintype, String name, Type baseType, Type[] interfaces, ReturnReferenceExpression returnType, params ArgumentReference[] args) { _typebuilder = maintype.TypeBuilder.DefineNestedType( name, TypeAttributes.Sealed|TypeAttributes.NestedPublic|TypeAttributes.Class, baseType, interfaces); }
internal EasyMethod( AbstractEasyType maintype, String name, MethodAttributes attrs, ReturnReferenceExpression returnRef, params ArgumentReference[] arguments ) { _maintype = maintype; _arguments = arguments; Type returnType = returnRef.Type; Type[] args = ArgumentsUtil.InitializeAndConvert( arguments ); _builder = maintype.TypeBuilder.DefineMethod( name, attrs, returnType, args ); }
public EasyCallable( AbstractEasyType type, int id, ReturnReferenceExpression returnType, params ArgumentReference[] args) : base(type, String.Format("__delegate_{0}", id), typeof(MulticastDelegate), new Type[] { typeof(ICallable) }, returnType, args) { _id = id; _args = args; _returnType = returnType; GenerateConstructor(); GenerateInvoke(); GenerateCallableImplementation(); }
public EasyRuntimeMethod CreateRuntimeMethod( String name, ReturnReferenceExpression returnType, params ArgumentReference[] arguments ) { EasyRuntimeMethod member = new EasyRuntimeMethod( this, name, returnType, arguments ); _methods.Add(member); return member; }
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; }
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 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 ); }
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 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(); }
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(); }
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(); }
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(); }