Example #1
0
        public void ParameterlessFunctionCall()
        {
            ILTest obj  = new ILTest();
            Type   type = typeof(ILTest);


            MethodInfo mi = type.GetMethod("WriteMessage");

            ILDelegates.MethodDel write = ILTools.GetMethodDel <ILDelegates.MethodDel>(type, mi);
            object funcCallSanityCheck  = write(obj);

            if (funcCallSanityCheck is string funcRet)
            {
                if (funcRet != "")
                {
                    throw new Exception();
                }
            }

            RunBenchmark("Function Call", "Reflection", "IL",
                         () =>
            {
                object methRet = mi.Invoke(obj, new object[0]);
            },
                         () =>
            {
                object methRet = write(obj);
            });
        }
Example #2
0
        public void GetPropertyValue()
        {
            ILTest       obj  = new ILTest();
            Type         type = typeof(ILTest);
            PropertyInfo pi   = type.GetProperty("PropertyField");

            ILDelegates.MethodDel getPF = ILTools.GetPropertyGet <ILDelegates.MethodDel>(type, pi);


            object getPropertySanityCheck = getPF(obj);

            if (getPropertySanityCheck is string propertyValue)
            {
                if (propertyValue != "456")
                {
                    throw new Exception();
                }
            }

            RunBenchmark("Get Property Value", "Reflection", "IL",
                         () =>
            {
                object ret = pi.GetValue(obj);
            },
                         () =>
            {
                object ret = getPF(obj);
            });
        }
Example #3
0
        public void SetPropertyValue()
        {
            ILTest       obj  = new ILTest();
            Type         type = typeof(ILTest);
            PropertyInfo pi   = type.GetProperty("PropertyField");


            ILDelegates.MethodDel_v <string> setPF = ILTools.GetPropertySet <ILDelegates.MethodDel_v <string> >(type, pi);

            setPF(obj, "EEEEEEE");
            if (obj.PropertyField != "EEEEEEE")
            {
                throw new Exception();
            }


            RunBenchmark("Set Property Value", "Reflection", "IL",
                         () =>
            {
                pi.SetValue(obj, "EEEEEEE");
            },
                         () =>
            {
                setPF(obj, "EEEEEEE");
            });
        }
        private void CompileAndGetIL(string sourceCode)
        {
            SetDescription("Compiling code...");

            _compilationTask = Task.Factory.StartNew(() =>
            {
                try
                {
                    var compiledMethod = Compiler.Compile(sourceCode);

                    var methodIL = ILTools.GetMethodIL(compiledMethod.Method);

                    ilList.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        ilList.Items.Clear();
                        foreach (var ilInstruction in methodIL.Instructions)
                        {
                            ilList.Items.Add(ilInstruction);
                        }

                        SetDescription("Press F5 to compile");
                    }), null);
                }
                catch (Exception ex)
                {
                    SetDescription("Press F5 to compile");
                    MessageBox.Show("Could not compile.\r\n" + ex, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });
        }
Example #5
0
        public void GetFieldValueTest()
        {
            ILTest    obj  = new ILTest();
            Type      type = typeof(ILTest);
            FieldInfo fi   = type.GetField("ValueField");


            ILDelegates.MethodDel fv = ILTools.GetFieldValue <ILDelegates.MethodDel>(type, fi);
            object fieldSanityCheck  = fi.GetValue(obj);

            if (fieldSanityCheck is string str)
            {
                if (str != "123")
                {
                    throw new Exception();
                }
            }

            RunBenchmark("Getting Field Value", "Reflection", "IL",
                         () =>
            {
                object ret = fi.GetValue(obj);
            },
                         () =>
            {
                object ret = fv(obj);
            });
        }
Example #6
0
        private void InitAspectProperties(CustomAttribute aspect, VariableDefinition aspectInstanceVar)
        {
            var ctor = initCtor.Body.Instructions;

            // init all array-type properties as local variables first
            var arrayVars = new Dictionary <CustomAttributeNamedArgument, VariableDefinition>();

            foreach (var prop in aspect.Properties.Where(a => a.Argument.Type.IsArray))
            {
                arrayVars.Add(prop, AddInitArrayCode(prop.Argument.Type, (Array)prop.Argument.Value));
            }

            // set field values
            // aspectVar.field1 = const1;
            foreach (var prop in aspect.Properties)
            {
                ctor.Add(OpCodes.Ldloc, aspectInstanceVar);
                if (prop.Argument.Type.IsArray)
                {
                    ctor.Add(OpCodes.Ldloc, arrayVars[prop]);
                }
                else
                {
                    ctor.Add(ILTools.GetLdcOpCode(prop.Argument.Type, prop.Argument.Value));
                }

                var propSetMethod = aspectInstanceVar.VariableType.FindProperty(prop.Name).SetMethod;
                ctor.Add(OpCodes.Callvirt, initCtor.Module.Import(propSetMethod));
            }
        }
Example #7
0
        private VariableDefinition CreateAspectInstance(CustomAttribute aspect)
        {
            var ctor = initCtor.Body.Instructions;

            // init all array-type parameters as local variables first
            var arrayVars = new Dictionary <CustomAttributeArgument, VariableDefinition>();

            foreach (var arg in aspect.ConstructorArguments.Where(a => a.Type.IsArray))
            {
                arrayVars.Add(arg, AddInitArrayCode(arg.Type, (Array)arg.Value));
            }

            // call aspect's contructor
            // var aspect = new AspectClass (param1, param2, ...)
            foreach (var arg in aspect.ConstructorArguments)
            {
                if (arg.Type.IsArray)
                {
                    ctor.Add(OpCodes.Ldloc, arrayVars[arg]);
                }
                else
                {
                    ctor.Add(ILTools.GetLdcOpCode(arg.Type, arg.Value));
                }
            }
            ctor.Add(OpCodes.Newobj, aspect.Constructor);

            var aspectInstanceVar = new VariableDefinition(aspect.AttributeType);

            ctor.Add(OpCodes.Stloc, aspectInstanceVar);
            initCtor.Body.Variables.Add(aspectInstanceVar);

            return(aspectInstanceVar);
        }
Example #8
0
        private void InitAspectFields(CustomAttribute aspect, VariableDefinition aspectInstanceVar)
        {
            var ctor = initCtor.Body.Instructions;

            // init all array-type fields as local variables first
            var arrayVars = new Dictionary <CustomAttributeNamedArgument, VariableDefinition>();

            foreach (var field in aspect.Fields.Where(a => a.Argument.Type.IsArray))
            {
                arrayVars.Add(field, AddInitArrayCode(field.Argument.Type, (Array)field.Argument.Value));
            }

            // set field values
            // aspectVar.field1 = const1;
            foreach (var field in aspect.Fields)
            {
                ctor.Add(OpCodes.Ldloc, aspectInstanceVar);
                if (field.Argument.Type.IsArray)
                {
                    ctor.Add(OpCodes.Ldloc, arrayVars[field]);
                }
                else
                {
                    ctor.Add(ILTools.GetLdcOpCode(field.Argument.Type, field.Argument.Value));
                }

                var fieldDef = aspectInstanceVar.VariableType.FindField(field.Name);
                ctor.Add(OpCodes.Stfld, initCtor.Module.Import(fieldDef));
            }
        }
Example #9
0
        public static void ILConstructorTest(int iters)
        {
            Type type = typeof(ILTest);

            ILDelegates.TypeConstructor ctor = ILTools.GetConstructor <ILDelegates.TypeConstructor>(type);

            object test = ctor();

            if (!(test is ILTest))
            {
                throw new Exception();
            }

            RunTest("Constructor", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object obj = Activator.CreateInstance(type);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object obj = ctor();
                }
            }
                    );
        }
Example #10
0
 public void Setup()
 {
     TestType     = typeof(ILTest);
     ctor         = ILTools.GetConstructor <ILDelegates.TypeConstructor>(TestType);
     fi           = TestType.GetField("ValueField");
     fv           = ILTools.GetFieldValue <ILDelegates.MethodDel>(TestType, fi);
     instance     = (ILTest)ctor();
     pi           = TestType.GetProperty("PropertyField"); getPropVal = ILTools.GetPropertyGet <ILDelegates.MethodDel>(TestType, pi);
     setPropVal   = ILTools.GetPropertySet <ILDelegates.MethodDel_v <string> >(TestType, pi);
     paramLessMi  = TestType.GetMethod("WriteMessage");
     paramLessDel = ILTools.GetMethodDel <ILDelegates.MethodDel>(TestType, paramLessMi);
     paramMi      = TestType.GetMethod("TestMethod");
     paramDel     = ILTools.GetMethodDel <ILDelegates.MethodDel_v <int> >(TestType, paramMi);
 }
        private void InitMethodExecInfoVar()
        {
            // The code below is IL generated from:
            // var methodExecInfo = new MethodExecInfo (new object[] {arg1, arg2, ...}, Method_0)
            var argsArrVar = new VariableDefinition(method.Module.Import(typeof(object[])));

            method.Body.Variables.Add(argsArrVar);

            methodExecInfoVar = new VariableDefinition(method.Module.Import(typeof(MethodExecInfo)));
            method.Body.Variables.Add(methodExecInfoVar);

            // arguments
            var code = new Collection <Instruction>();

            code.Add(OpCodes.Ldc_I4, method.Parameters.Count);
            code.Add(OpCodes.Newarr, method.Module.Import(typeof(object)));
            code.Add(OpCodes.Stloc, argsArrVar);
            code.Add(OpCodes.Ldloc, argsArrVar);
            for (int i = 0; i < method.Parameters.Count; ++i)
            {
                var param     = method.Parameters[i];
                var paramType = param.ParameterType;

                code.Add(OpCodes.Ldc_I4, i);
                code.Add(OpCodes.Ldarg, param);

                if (paramType is ByReferenceType)
                {
                    paramType = ((ByReferenceType)paramType).ElementType;
                    code.Add(ILTools.GetLdindOpCode(paramType));
                }

                if (paramType.IsValueType || paramType.IsGenericParameter)
                {
                    code.Add(OpCodes.Box, paramType);
                }

                code.Add(OpCodes.Stelem_Ref);
                code.Add(OpCodes.Ldloc, argsArrVar);
            }

            // method info
            code.Add(OpCodes.Ldsfld, methodStaticField);

            code.Add(OpCodes.Newobj, method.Module.Import(typeof(MethodExecInfo).GetConstructor(new[] { typeof(object[]), typeof(MethodBase) })));
            code.Add(OpCodes.Stloc, methodExecInfoVar);

            method.Body.Instructions.Insert(0, code);
        }
Example #12
0
        public void FunctionCall()
        {
            ILTest     obj      = new ILTest();
            Type       type     = typeof(ILTest);
            MethodInfo testMeth = type.GetMethod("TestMethod");



            ILDelegates.MethodDel_v <int> func = ILTools.GetMethodDel <ILDelegates.MethodDel_v <int> >(type, testMeth);

            RunBenchmark("Function Call with Parameter", "Reflection", "IL",
                         () =>
            {
                testMeth.Invoke(obj, new object[] { 100 });
            },
                         () =>
            {
                func(obj, 100);
            });
        }
Example #13
0
        public void ILConstructorTest()
        {
            Type type = typeof(ILTest);

            ILDelegates.TypeConstructor ctor = ILTools.GetConstructor <ILDelegates.TypeConstructor>(type);

            object test = ctor();

            if (!(test is ILTest))
            {
                throw new Exception();
            }

            RunBenchmark("Constructor", "Reflection", "IL", () =>
            {
                object obj = Activator.CreateInstance(type);
            }, () =>
            {
                object obj = ctor();
            });
        }
Example #14
0
        private VariableDefinition AddInitArrayCode(TypeReference type, Array value)
        {
            if (value.Rank != 1)
            {
                throw new ArgumentException("Only 1-dimension arrays are supported", "value");
            }

            var ctor     = initCtor.Body.Instructions;
            var elemType = type.GetElementType();

            // Generates code:
            // var arrayVar = new ArrayType[n];
            // arrayVar[0] = const1;
            // arrayVar[1] = const2;
            // ...

            var arrayVar = new VariableDefinition(type);

            initCtor.Body.Variables.Add(arrayVar);

            int n = value.GetLength(0);

            ctor.Add(OpCodes.Ldc_I4, n);
            ctor.Add(OpCodes.Newarr, elemType);
            ctor.Add(OpCodes.Stloc, arrayVar);

            int startIndex = value.GetLowerBound(0);

            for (int i = 0; i < n; ++i)
            {
                ctor.Add(OpCodes.Ldloc, arrayVar);
                ctor.Add(OpCodes.Ldc_I4, i + startIndex);
                ctor.Add(ILTools.GetLdcOpCode(elemType, ((CustomAttributeArgument)value.GetValue(i + startIndex)).Value));
                ctor.Add(ILTools.GetStelemOpCode(elemType));
            }

            return(arrayVar);
        }
 public static MethodILInfo GetInstructions(this MethodInfo methodInfo)
 {
     return(ILTools.GetMethodIL(methodInfo));
 }
Example #16
0
        public static void ILTests(int iters)
        {
            ILConstructorTest(iters);


            Type type = typeof(ILTest);

            ILDelegates.TypeConstructor ctor = ILTools.GetConstructor <ILDelegates.TypeConstructor>(type);
            FieldInfo    fi       = type.GetField("ValueField");
            PropertyInfo pi       = type.GetProperty("PropertyField");
            MethodInfo   mi       = type.GetMethod("WriteMessage");
            MethodInfo   testMeth = type.GetMethod("TestMethod");



            ILTest obj = (ILTest)ctor();

            obj.ValueField    = "123";
            obj.PropertyField = "456";

            ILDelegates.MethodDel fv = ILTools.GetFieldValue <ILDelegates.MethodDel>(type, fi);
            object fieldSanityCheck  = fi.GetValue(obj);

            if (fieldSanityCheck is string str)
            {
                if (str != "123")
                {
                    throw new Exception();
                }
            }

            RunTest("Getting Field Value", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = fi.GetValue(obj);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = fv(obj);
                }
            });


            ILDelegates.MethodDel getPF = ILTools.GetPropertyGet <ILDelegates.MethodDel>(type, pi);

            object getPropertySanityCheck = getPF(obj);

            if (getPropertySanityCheck is string propertyValue)
            {
                if (propertyValue != "456")
                {
                    throw new Exception();
                }
            }

            RunTest("Get Property Value", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = pi.GetValue(obj);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = getPF(obj);
                }
            });



            ILDelegates.MethodDel_v <string> setPF = ILTools.GetPropertySet <ILDelegates.MethodDel_v <string> >(type, pi);

            setPF(obj, "EEEEEEE");
            if (obj.PropertyField != "EEEEEEE")
            {
                throw new Exception();
            }


            RunTest("Set Property Value", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    pi.SetValue(obj, "EEEEEEE");
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    setPF(obj, "EEEEEEE");
                }
            });

            ILDelegates.MethodDel write = ILTools.GetMethodDel <ILDelegates.MethodDel>(type, mi);
            object funcCallSanityCheck  = write(obj);

            if (funcCallSanityCheck is string funcRet)
            {
                if (funcRet != "")
                {
                    throw new Exception();
                }
            }

            RunTest("Function Call", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object methRet = mi.Invoke(obj, new object[0]);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object methRet = write(obj);
                }
            });


            ILDelegates.MethodDel_v <int> func = ILTools.GetMethodDel <ILDelegates.MethodDel_v <int> >(type, testMeth);

            RunTest("Function Call with Parameter", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    testMeth.Invoke(obj, new object[] { 100 });
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    func(obj, 100);
                }
            });
        }