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 EasyMethod CreateMethod(string name, MethodAttributes attrs, ReturnReferenceExpression returnType, params Type[] args)
        {
            EasyMethod method = new EasyMethod(this, name, attrs, returnType, ArgumentsUtil.ConvertToArgumentReference(args));

            this._methods.Add(method);
            return(method);
        }
        public EasyRuntimeMethod CreateRuntimeMethod(string name, ReturnReferenceExpression returnType, params ArgumentReference[] arguments)
        {
            EasyRuntimeMethod method = new EasyRuntimeMethod(this, name, returnType, arguments);

            this._methods.Add(method);
            return(method);
        }
        public EasyMethod CreateMethod(string name, ReturnReferenceExpression returnType, MethodAttributes attributes, params ArgumentReference[] arguments)
        {
            EasyMethod method = new EasyMethod(this, name, attributes, returnType, arguments);

            this._methods.Add(method);
            return(method);
        }
Esempio n. 5
0
        public EasyCallable CreateCallable(ReturnReferenceExpression returnType, params ArgumentReference[] args)
        {
            EasyCallable nested = new EasyCallable(this, base.IncrementAndGetCounterValue, returnType, args);

            base._nested.Add(nested);
            return(nested);
        }
Esempio n. 6
0
        public EasyMethod CreateMethod(String name, ReturnReferenceExpression returnType, params ArgumentReference[] arguments)
        {
            EasyMethod member = new EasyMethod(this, name, returnType, arguments);

            _methods.Add(member);
            return(member);
        }
Esempio n. 7
0
 internal EasyMethod(AbstractEasyType maintype, String name,
                     ReturnReferenceExpression returnRef, params ArgumentReference[] arguments) :
     this(maintype, name,
          MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public,
          returnRef, arguments)
 {
 }
        public void NewInstanceExpression()
        {
            EasyType typebuilder = new EasyType(module, "mytype");

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

            EasyConstructor constructor = typebuilder.CreateConstructor( );

            constructor.CodeBuilder.InvokeBaseConstructor();
            constructor.CodeBuilder.AddStatement(new AssignStatement(cachefield,
                                                                     new NewInstanceExpression(typeof(ArrayList), new Type[0])));
            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 MethodCalc()
        {
            EasyType typebuilder = new EasyType(module, "mytype");

            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 BinaryExpression(BinaryExpression.Add, arg1.ToExpression(), arg2.ToExpression())));

            Type newType = typebuilder.BuildType();

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

            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 }));

            RunPEVerify();
        }
Esempio n. 10
0
        public EasyRuntimeMethod(AbstractEasyType maintype, string name, ReturnReferenceExpression returnRef, ArgumentReference[] arguments)
        {
            MethodAttributes attributes = MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public;

            Type[] parameterTypes = ArgumentsUtil.InitializeAndConvert(arguments);
            base._builder = maintype.TypeBuilder.DefineMethod(name, attributes, returnRef.Type, parameterTypes);
            base._builder.SetImplementationFlags(MethodImplAttributes.CodeTypeMask);
        }
Esempio n. 11
0
 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)
 {
     this._id         = id;
     this._args       = args;
     this._returnType = returnType;
     this.GenerateConstructor();
     this.GenerateInvoke();
     this.GenerateCallableImplementation();
 }
Esempio n. 12
0
 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);
 }
        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 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);
        }
Esempio n. 15
0
        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 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 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();
        }
        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();
        }