コード例 #1
0
        public unsafe void SetX_NullInput_ThrowsArgumentNullException(bool skipVisibility)
        {
            DynamicMethod method        = GetDynamicMethod(skipVisibility);
            DynamicILInfo dynamicILInfo = method.GetDynamicILInfo();

            Assert.Throws <ArgumentNullException>(() => dynamicILInfo.SetCode(null, 1, 8));
            Assert.Throws <ArgumentNullException>(() => dynamicILInfo.SetExceptions(null, 1));
            Assert.Throws <ArgumentNullException>(() => dynamicILInfo.SetLocalSignature(null, 1));
        }
コード例 #2
0
        private static void SetCode(MethodBase method, MethodBody body, DynamicILInfo ilInfo)
        {
            var code    = body.GetILAsByteArray();
            var reader  = new ILReader.ILReader(method);
            var visitor = new IlInfoGetTokenVisitor(ilInfo, code);

            reader.Accept(visitor);
            ilInfo.SetCode(code, body.MaxStackSize);
        }
コード例 #3
0
        public void GetTokenFor_Exception_Success()
        {
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(ExceptionTest), typeof(int), Type.EmptyTypes, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(string), false);
            sigHelper.AddArgument(typeof(int), false);
            dynamicILInfo.SetLocalSignature(sigHelper.GetSignature());

            byte[] code =
            {
                0x00, 0x16, 0x0a, 0x00, 0x00, 0x16, 0x0b, 0x17, 0x07, 0x5b, 0x0c, 0x00, 0xde, 0x09, 0x26, 0x00,
                0x06, 0x17, 0x58, 0x0a, 0x00, 0xde, 0x00, 0x00, 0x00, 0x72, 0xed, 0x01, 0x00, 0x70, 0x17, 0x28,
                0x32, 0x00, 0x00, 0x0a, 0x26, 0x00, 0xde, 0x09, 0x26, 0x00, 0x06, 0x17, 0x58, 0x0a, 0x00, 0xde,
                0x00, 0x00, 0x14, 0x0d, 0x09, 0x6f, 0x05, 0x00, 0x00, 0x0a, 0x26, 0x00, 0xde, 0x09, 0x26, 0x00,
                0x06, 0x17, 0x58, 0x0a, 0x00, 0xde, 0x00, 0x00, 0xde, 0x07, 0x00, 0x06, 0x18, 0x58, 0x0a, 0x00,
                0xdc, 0x00, 0x06, 0x13, 0x04, 0x2b, 0x00, 0x11, 0x04, 0x2a
            };
            int token0 = dynamicILInfo.GetTokenFor("A.B");
            int token1 = dynamicILInfo.GetTokenFor(typeof(System.Type).GetMethod("GetType", new Type[] { typeof(string), typeof(bool) }).MethodHandle);
            int token2 = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("ToUpper", Type.EmptyTypes).MethodHandle);

            PutInteger4(token0, 0x001a, code);
            PutInteger4(token1, 0x0020, code);
            PutInteger4(token2, 0x0036, code);
            dynamicILInfo.SetCode(code, 2);

            int token3 = dynamicILInfo.GetTokenFor(typeof(System.Object).TypeHandle);
            int token4 = dynamicILInfo.GetTokenFor(typeof(System.Exception).TypeHandle);
            int token5 = dynamicILInfo.GetTokenFor(typeof(System.NullReferenceException).TypeHandle);

            byte[] exceptions =
            {
                0x41, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
                0x0e, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x18, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x00, 0x00,
                0x3e, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
                0x03, 0x00, 0x00, 0x00, 0x47, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00
            };
            PutInteger4(token3, 0x0018, exceptions);
            PutInteger4(token4, 0x0030, exceptions);
            PutInteger4(token5, 0x0048, exceptions);
            dynamicILInfo.SetExceptions(exceptions);

            int ret = (int)dynamicMethod.Invoke(null, null);

            Assert.Equal(ret, ExceptionTest());
        }
コード例 #4
0
        public void GetTokenFor_IntGenerics_Success()
        {
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(SumInteger), typeof(int), new Type[] { }, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            sigHelper.AddArgument(typeof(MyList <int>), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(System.Collections.Generic.IEnumerator <int>), false);
            sigHelper.AddArgument(typeof(bool), false);
            dynamicILInfo.SetLocalSignature(sigHelper.GetSignature());

            byte[] code =
            {
                0x00, 0x73, 0x1c, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x1f, 0x64, 0x6f, 0x1d, 0x00, 0x00, 0x0a, 0x00,
                0x06, 0x20, 0xc8, 0x00, 0x00, 0x00, 0x6f, 0x1d, 0x00, 0x00, 0x0a, 0x00, 0x06, 0x20, 0x2c, 0x01,
                0x00, 0x00, 0x6f, 0x1d, 0x00, 0x00, 0x0a, 0x00, 0x16, 0x0b, 0x00, 0x06, 0x6f, 0x1e, 0x00, 0x00,
                0x0a, 0x13, 0x04, 0x2b, 0x0e, 0x11, 0x04, 0x6f, 0x1f, 0x00, 0x00, 0x0a, 0x0c, 0x00, 0x07, 0x08,
                0x58, 0x0b, 0x00, 0x11, 0x04, 0x6f, 0x20, 0x00, 0x00, 0x0a, 0x13, 0x05, 0x11, 0x05, 0x2d, 0xe5,
                0xde, 0x14, 0x11, 0x04, 0x14, 0xfe, 0x01, 0x13, 0x05, 0x11, 0x05, 0x2d, 0x08, 0x11, 0x04, 0x6f,
                0x21, 0x00, 0x00, 0x0a, 0x00, 0xdc, 0x00, 0x07, 0x0d, 0x2b, 0x00, 0x09, 0x2a
            };
            int token0 = dynamicILInfo.GetTokenFor(typeof(MyList <int>).GetConstructors()[0].MethodHandle, typeof(MyList <int>).TypeHandle);
            int token1 = dynamicILInfo.GetTokenFor(typeof(MyList <int>).GetMethod("Add").MethodHandle, typeof(MyList <int>).TypeHandle);
            int token2 = dynamicILInfo.GetTokenFor(typeof(MyList <int>).GetMethod("GetEnumerator").MethodHandle, typeof(MyList <int>).TypeHandle);
            int token3 = dynamicILInfo.GetTokenFor(typeof(System.Collections.Generic.IEnumerator <int>).GetMethod("get_Current").MethodHandle, typeof(System.Collections.Generic.IEnumerator <int>).TypeHandle);
            int token4 = dynamicILInfo.GetTokenFor(typeof(System.Collections.IEnumerator).GetMethod("MoveNext").MethodHandle);
            int token5 = dynamicILInfo.GetTokenFor(typeof(System.IDisposable).GetMethod("Dispose").MethodHandle);

            PutInteger4(token0, 0x0002, code);
            PutInteger4(token1, 0x000b, code);
            PutInteger4(token1, 0x0017, code);
            PutInteger4(token1, 0x0023, code);
            PutInteger4(token2, 0x002d, code);
            PutInteger4(token3, 0x0038, code);
            PutInteger4(token4, 0x0046, code);
            PutInteger4(token5, 0x0060, code);
            dynamicILInfo.SetCode(code, 2);

            byte[] exceptions =
            {
                0x41, 0x1c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00,
                0x52, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            dynamicILInfo.SetExceptions(exceptions);

            int ret = (int)dynamicMethod.Invoke(null, null);

            Assert.Equal(ret, SumInteger());
        }
コード例 #5
0
        /// <summary>
        /// Binds this method body to a method dynamically generated with the DynamicMethod
        /// API.
        /// </summary>
        /// <param name="dynamicILInfo">The <see cref="DynamicILInfo"/> for the dynamic method.</param>
        public void bindToDynamicILInfo(DynamicILInfo dynamicILInfo)
        {
            dynamicILInfo.SetCode(m_methodBody, m_maxStack);

            // DynamicILInfo requires a valid local signature, so use the empty signature
            // if there are no locals.
            dynamicILInfo.SetLocalSignature((m_localSignature.Length != 0) ? m_localSignature : s_emptyLocalSig);

            if (m_ehSection.Length != 0)
            {
                dynamicILInfo.SetExceptions(m_ehSection);
            }
        }
コード例 #6
0
        public void Test_TwoDimTest()
        {
            // 2-D array (set/address/get)
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(TwoDimTest), typeof(int), Type.EmptyTypes, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(System.Int32[, ]), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(bool), false);
            dynamicILInfo.SetLocalSignature(sigHelper.GetSignature());

            byte[] code =
            {
                0x00, 0x1c, 0x0a, 0x1e, 0x0b, 0x06, 0x07, 0x73, 0x3e, 0x00, 0x00, 0x0a, 0x0c, 0x16, 0x0d, 0x2b,
                0x27, 0x16, 0x13, 0x04, 0x2b, 0x13, 0x08, 0x09, 0x11, 0x04, 0x09, 0x11, 0x04, 0x5a, 0x28, 0x3f,
                0x00, 0x00, 0x0a, 0x11, 0x04, 0x17, 0x58, 0x13, 0x04, 0x11, 0x04, 0x07, 0xfe, 0x04, 0x13, 0x06,
                0x11, 0x06, 0x2d, 0xe2, 0x09, 0x17, 0x58, 0x0d, 0x09, 0x06, 0xfe, 0x04, 0x13, 0x06, 0x11, 0x06,
                0x2d, 0xcf, 0x17, 0x0d, 0x2b, 0x3c, 0x17, 0x13, 0x04, 0x2b, 0x28, 0x08, 0x09, 0x11, 0x04, 0x28,
                0x40, 0x00, 0x00, 0x0a, 0x25, 0x71, 0x1f, 0x00, 0x00, 0x01, 0x08, 0x09, 0x17, 0x59, 0x11, 0x04,
                0x17, 0x59, 0x28, 0x41, 0x00, 0x00, 0x0a, 0x58, 0x81, 0x1f, 0x00, 0x00, 0x01, 0x11, 0x04, 0x17,
                0x58, 0x13, 0x04, 0x11, 0x04, 0x07, 0xfe, 0x04, 0x13, 0x06, 0x11, 0x06, 0x2d, 0xcd, 0x09, 0x17,
                0x58, 0x0d, 0x09, 0x06, 0xfe, 0x04, 0x13, 0x06, 0x11, 0x06, 0x2d, 0xba, 0x08, 0x06, 0x17, 0x59,
                0x07, 0x17, 0x59, 0x28, 0x41, 0x00, 0x00, 0x0a, 0x13, 0x05, 0x2b, 0x00, 0x11, 0x05, 0x2a
            };
            int token0 = dynamicILInfo.GetTokenFor(typeof(System.Int32[, ]).GetConstructor(new Type[] { typeof(int), typeof(int) }).MethodHandle);
            int token1 = dynamicILInfo.GetTokenFor(typeof(System.Int32[, ]).GetMethod("Set").MethodHandle);
            int token2 = dynamicILInfo.GetTokenFor(typeof(System.Int32[, ]).GetMethod("Address").MethodHandle);
            int token3 = dynamicILInfo.GetTokenFor(typeof(int).TypeHandle);
            int token4 = dynamicILInfo.GetTokenFor(typeof(System.Int32[, ]).GetMethod("Get").MethodHandle);

            PutInteger4(token0, 0x0008, code);
            PutInteger4(token1, 0x001f, code);
            PutInteger4(token2, 0x0050, code);
            PutInteger4(token3, 0x0056, code);
            PutInteger4(token4, 0x0063, code);
            PutInteger4(token3, 0x0069, code);
            PutInteger4(token4, 0x0094, code);
            dynamicILInfo.SetCode(code, 6);

            int ret = (int)dynamicMethod.Invoke(null, null);

            Assert.Equal(ret, TwoDimTest());
        }
コード例 #7
0
        // Token: 0x060000FC RID: 252 RVA: 0x0000A9F4 File Offset: 0x00008BF4
        private static void c792380fd21eca6e08b9631600cc2d331(ref int ca0c61e7592f10963057a6d5e207fa679, MethodBase c349afe24756c2022fc84606a1e6d3419, DynamicILInfo c3c57c9acc0a08dbaa5fcc9490f2a3eac)
        {
            int maxStackSize = BitConverter.ToInt32(c112201421a408a8f2963cee12a5d2e69.c271a832e629a79991d9eda7a270cb125, ca0c61e7592f10963057a6d5e207fa679);

            ca0c61e7592f10963057a6d5e207fa679 += 4;
            int num = BitConverter.ToInt32(c112201421a408a8f2963cee12a5d2e69.c271a832e629a79991d9eda7a270cb125, ca0c61e7592f10963057a6d5e207fa679);

            ca0c61e7592f10963057a6d5e207fa679 += 4;
            byte[] array = c4cbbd90ed4559089f2970be8fc52599d.cad714a49573fd585a8ab14c80a23536b(num);
            Buffer.BlockCopy(c112201421a408a8f2963cee12a5d2e69.c271a832e629a79991d9eda7a270cb125, ca0c61e7592f10963057a6d5e207fa679, array, 0, num);
            c112201421a408a8f2963cee12a5d2e69.ccc46268e89df4ba9fc0a02a649177e87 ccc46268e89df4ba9fc0a02a649177e = new c112201421a408a8f2963cee12a5d2e69.ccc46268e89df4ba9fc0a02a649177e87(c349afe24756c2022fc84606a1e6d3419, array, c3c57c9acc0a08dbaa5fcc9490f2a3eac);
            ccc46268e89df4ba9fc0a02a649177e.ce88c13cebaf30d6cda3a6382e8991b8d();
            c3c57c9acc0a08dbaa5fcc9490f2a3eac.SetCode(array, maxStackSize);
            ca0c61e7592f10963057a6d5e207fa679 += num;
        }
コード例 #8
0
        public void GetTokenFor_String()
        {
            DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTest), false);
            DynamicILInfo il = dm.GetDynamicILInfo();

            byte[] code   = { 0x00, 0x72, 0x01, 0x00, 0x00, 0x70, 0x2a };
            int    token0 = il.GetTokenFor("ABCD");

            PutInteger4(token0, 0x0002, code);
            il.SetCode(code, 8);

            var res = dm.Invoke(null, null);

            Assert.AreEqual("ABCD", res);
        }
コード例 #9
0
        public void GetTokenFor_DynamicMethod()
        {
            DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(RuntimeMethodHandle), Type.EmptyTypes, typeof(DynamicILInfoTest), false);
            DynamicILInfo il = dm.GetDynamicILInfo();

            byte[] code   = { 0x00, 0xd0, 0x01, 0x00, 0x00, 0x70, 0x2a };
            int    token0 = il.GetTokenFor(dm);

            PutInteger4(token0, 0x0002, code);
            il.SetCode(code, 8);

            var res = dm.Invoke(null, null);

            Assert.AreEqual(dm.MethodHandle, res);
        }
コード例 #10
0
        public void GetTokenFor_Type()
        {
            DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(Type), Type.EmptyTypes, typeof(DynamicILInfoTest), false);
            DynamicILInfo il = dm.GetDynamicILInfo();

            byte[] code   = { 0x00, 0xd0, 0x01, 0x00, 0x00, 0x70, 0x28, 0x04, 0x00, 0x00, 0x0a, 0x00, 0x2a };
            int    token0 = il.GetTokenFor(typeof(int).TypeHandle);
            int    token1 = il.GetTokenFor(typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) }).MethodHandle);

            PutInteger4(token0, 0x0002, code);
            PutInteger4(token1, 0x0007, code);
            il.SetCode(code, 8);

            var res = dm.Invoke(null, null);

            Assert.AreEqual(typeof(int), res);
        }
コード例 #11
0
        public void GetTokenFor_Method()
        {
            DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTest), false);
            DynamicILInfo il = dm.GetDynamicILInfo();

            // ldstr "ABCD"; call string::ToLower (); ret
            byte[] code   = { 0x00, 0x72, 0x01, 0x00, 0x00, 0x70, 0x28, 0x04, 0x00, 0x00, 0x0a, 0x00, 0x2a };
            int    token0 = il.GetTokenFor("ABCD");
            int    token1 = il.GetTokenFor(typeof(string).GetMethod("ToLower", Type.EmptyTypes).MethodHandle);

            PutInteger4(token0, 0x0002, code);
            PutInteger4(token1, 0x0007, code);
            il.SetCode(code, 8);

            var res = dm.Invoke(null, null);

            Assert.AreEqual("abcd", res);
        }
コード例 #12
0
        public void GetTokenFor_FieldInfo()
        {
            aField = aField + 1;
            DynamicMethod dm = new DynamicMethod("HelloWorld", typeof(RuntimeFieldHandle), Type.EmptyTypes, typeof(DynamicILInfoTest), false);
            DynamicILInfo il = dm.GetDynamicILInfo();

            var f = typeof(DynamicILInfoTest).GetField("aField", BindingFlags.Static | BindingFlags.NonPublic).FieldHandle;

            byte[] code   = { 0x00, 0xd0, 0x01, 0x00, 0x00, 0x70, 0x2a };
            int    token0 = il.GetTokenFor(f);

            PutInteger4(token0, 0x0002, code);
            il.SetCode(code, 8);

            var res = dm.Invoke(null, null);

            Assert.AreEqual(f, res);
        }
コード例 #13
0
        public unsafe void SetX_NegativeInputSize_ThrowsArgumentOutOfRangeException(bool skipVisibility)
        {
            DynamicMethod method        = GetDynamicMethod(skipVisibility);
            DynamicILInfo dynamicILInfo = method.GetDynamicILInfo();
            var           bytes         = new byte[] { 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 };

            Assert.Throws <ArgumentOutOfRangeException>(() => { fixed(byte *bytesPtr = bytes)
                                                                {
                                                                    dynamicILInfo.SetCode(bytesPtr, -1, 8);
                                                                } });
            Assert.Throws <ArgumentOutOfRangeException>(() => { fixed(byte *bytesPtr = bytes)
                                                                {
                                                                    dynamicILInfo.SetExceptions(bytesPtr, -1);
                                                                } });
            Assert.Throws <ArgumentOutOfRangeException>(() => { fixed(byte *bytesPtr = bytes)
                                                                {
                                                                    dynamicILInfo.SetLocalSignature(bytesPtr, -1);
                                                                } });
        }
コード例 #14
0
        public ExecuteExpression <T> CreateExpressionDelegate <T>(DynamicMethodState methodState, object functionClass)
        {
            ExecuteExpression <T> expressionDelegate;
            Stopwatch             stopWatch = new Stopwatch();

            Trace.WriteLine("Dynamic dll creation - " + stopWatch.ElapsedMilliseconds.ToString());

            stopWatch = new Stopwatch();

            stopWatch.Start();

            //create
            DynamicMethod dynamicMethod = new DynamicMethod("", typeof(T), new Type[] { functionClass.GetType() }, functionClass.GetType());

            DynamicILInfo dynamicInfo = dynamicMethod.GetDynamicILInfo();

            dynamicMethod.InitLocals = methodState.InitLocals;

            SignatureHelper locals = SignatureHelper.GetLocalVarSigHelper();

            foreach (int localIndex in methodState.LocalVariables.Keys)
            {
                LocalVariable localVar = methodState.LocalVariables[localIndex];
                locals.AddArgument(localVar.LocalType, localVar.IsPinned);
            }

            dynamicInfo.SetLocalSignature(locals.GetSignature());

            IlTokenResolver tokenResolver = new IlTokenResolver(methodState.TokenOffset.Fields, methodState.TokenOffset.Methods, methodState.TokenOffset.Members, methodState.TokenOffset.Types, methodState.TokenOffset.LiteralStrings);

            methodState.CodeBytes = tokenResolver.ResolveCodeTokens(methodState.CodeBytes, dynamicInfo);

            dynamicInfo.SetCode(methodState.CodeBytes, methodState.MaxStackSize);

            expressionDelegate = (ExecuteExpression <T>)dynamicMethod.CreateDelegate(typeof(ExecuteExpression <T>), functionClass);

            stopWatch.Stop();

            Trace.WriteLine("Dynamic Method Creation - " + stopWatch.ElapsedMilliseconds.ToString());

            return(expressionDelegate);
        }
コード例 #15
0
        static void HelloFromDynamicILInfo()
        {
            DynamicMethod   dm        = new DynamicMethod("HelloWorld", typeof(void), Type.EmptyTypes, typeof(Foo), false);
            DynamicILInfo   il        = dm.GetDynamicILInfo();
            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            il.SetLocalSignature(sigHelper.GetSignature());
            byte[] code =
            {
                /* ldstr */ 0x72, 0x00, 0x00, 0x00, 0x00,
                /* call  */ 0x28, 0x00, 0x00, 0x00, 0x00,
                /* ret   */ 0x2a
            };
            byte[] strTokenBytes = BitConverter.GetBytes(il.GetTokenFor("Hello world!"));
            var    writeLine     = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });

            byte[] mTokenBytes = BitConverter.GetBytes(il.GetTokenFor(writeLine.MethodHandle));
            Array.Copy(strTokenBytes, 0, code, 1, strTokenBytes.Length);
            Array.Copy(mTokenBytes, 0, code, 5, mTokenBytes.Length);
            il.SetCode(code, 3);
            var hello = (Action)dm.CreateDelegate(typeof(Action));
        }
コード例 #16
0
        public void GetTokenFor_CtorMethodAndField_Success()
        {
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(Mock), typeof(Person), new Type[] { }, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            sigHelper.AddArgument(typeof(Person), false);
            sigHelper.AddArgument(typeof(Person), false);
            dynamicILInfo.SetLocalSignature(sigHelper.GetSignature());

            byte[] code =
            {
                0x00, 0x72, 0x49, 0x00, 0x00, 0x70, 0x1f, 0x32, 0x22, 0x00, 0x60, 0xea, 0x46, 0x73, 0x0f, 0x00,
                0x00, 0x06, 0x0a, 0x06, 0x72, 0x53, 0x00, 0x00, 0x70, 0x7d, 0x04, 0x00, 0x00, 0x04, 0x06, 0x25,
                0x6f, 0x0c, 0x00, 0x00, 0x06, 0x17, 0x58, 0x6f, 0x0d, 0x00, 0x00, 0x06, 0x00, 0x06, 0x22, 0x00,
                0x00, 0x96, 0x43, 0x6f, 0x10, 0x00, 0x00, 0x06, 0x00, 0x06, 0x0b, 0x2b, 0x00, 0x07, 0x2a
            };
            int token0 = dynamicILInfo.GetTokenFor("Bill");
            int token1 = dynamicILInfo.GetTokenFor(typeof(Person).GetConstructor(new Type[] { typeof(string), typeof(int), typeof(float) }).MethodHandle);
            int token2 = dynamicILInfo.GetTokenFor("Bill Gates");
            int token3 = dynamicILInfo.GetTokenFor(typeof(Person).GetField("m_name").FieldHandle);
            int token4 = dynamicILInfo.GetTokenFor(typeof(Person).GetMethod("get_Age").MethodHandle);
            int token5 = dynamicILInfo.GetTokenFor(typeof(Person).GetMethod("set_Age").MethodHandle);
            int token6 = dynamicILInfo.GetTokenFor(typeof(Person).GetMethod("IncSalary").MethodHandle);

            PutInteger4(token0, 0x0002, code);
            PutInteger4(token1, 0x000e, code);
            PutInteger4(token2, 0x0015, code);
            PutInteger4(token3, 0x001a, code);
            PutInteger4(token4, 0x0021, code);
            PutInteger4(token5, 0x0028, code);
            PutInteger4(token6, 0x0034, code);
            dynamicILInfo.SetCode(code, 4);

            Person ret = (Person)dynamicMethod.Invoke(null, null);

            Assert.Equal(ret, Mock());
        }
コード例 #17
0
        /// <summary>
        /// Compiles a DynamicMethodState and returns a delegate.
        /// </summary>
        /// <typeparam name="R">The return type of the expression</typeparam>
        /// <typeparam name="C">The type of the function class</typeparam>
        /// <param name="methodState">The serialized version of a method on the functionClass</param>
        /// <returns>ExecuteExpression&lt;R, C&gt; - a delegate that calls the compiled expression</returns>
        public ExecuteExpression <R, C> CreateExpressionDelegate <R, C>(DynamicMethodState methodState)
        {
            ExecuteExpression <R, C> expressionDelegate;

            //create a dynamic method
            DynamicMethod dynamicMethod = new DynamicMethod("_" + Guid.NewGuid().ToString("N"), typeof(R), new Type[] { typeof(C) }, typeof(C));

            //get the IL writer for it
            DynamicILInfo dynamicInfo = dynamicMethod.GetDynamicILInfo();

            //set the properties gathered from the compiled expression
            dynamicMethod.InitLocals = methodState.InitLocals;

            //set local variables
            SignatureHelper locals = SignatureHelper.GetLocalVarSigHelper();

            foreach (int localIndex in methodState.LocalVariables.Keys)
            {
                LocalVariable localVar = methodState.LocalVariables[localIndex];
                locals.AddArgument(Type.GetTypeFromHandle(localVar.LocalType), localVar.IsPinned);
            }

            dynamicInfo.SetLocalSignature(locals.GetSignature());

            //resolve any metadata tokens
            IlTokenResolver tokenResolver = new IlTokenResolver(methodState.TokenOffset.Fields, methodState.TokenOffset.Methods, methodState.TokenOffset.Types, methodState.TokenOffset.LiteralStrings);

            methodState.CodeBytes = tokenResolver.ResolveCodeTokens(methodState.CodeBytes, dynamicInfo);

            //set the IL code for the dynamic method
            dynamicInfo.SetCode(methodState.CodeBytes, methodState.MaxStackSize);

            //create a delegate for fast execution
            expressionDelegate = (ExecuteExpression <R, C>)dynamicMethod.CreateDelegate(typeof(ExecuteExpression <R, C>));

            return(expressionDelegate);
        }
コード例 #18
0
        internal static void Initialize(RuntimeFieldHandle field, byte opKey)
        {
            FieldInfo fieldInfo = FieldInfo.GetFieldFromHandle(field);

            byte[] sig = fieldInfo.Module.ResolveSignature(fieldInfo.MetadataToken);
            int    len = sig.Length;
            int    key = fieldInfo.GetOptionalCustomModifiers()[0].MetadataToken;

            key += (fieldInfo.Name[Mutation.KeyI0] ^ sig[--len]) << Mutation.KeyI4;
            key += (fieldInfo.Name[Mutation.KeyI1] ^ sig[--len]) << Mutation.KeyI5;
            key += (fieldInfo.Name[Mutation.KeyI2] ^ sig[--len]) << Mutation.KeyI6;
            len--;
            key += (fieldInfo.Name[Mutation.KeyI3] ^ sig[--len]) << Mutation.KeyI7;

            int token = Mutation.Placeholder(key);

            token *= fieldInfo.GetCustomAttributes(false)[0].GetHashCode();

            MethodBase method       = fieldInfo.Module.ResolveMethod(token);
            Type       delegateType = fieldInfo.FieldType;

            if (method.IsStatic)
            {
                fieldInfo.SetValue(null, Delegate.CreateDelegate(delegateType, (MethodInfo)method));
            }

            else
            {
                DynamicMethod dm       = null;
                Type[]        argTypes = null;

                foreach (MethodInfo invoke in fieldInfo.FieldType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    if (invoke.DeclaringType == delegateType)
                    {
                        ParameterInfo[] paramTypes = invoke.GetParameters();
                        argTypes = new Type[paramTypes.Length];
                        for (int i = 0; i < argTypes.Length; i++)
                        {
                            argTypes[i] = paramTypes[i].ParameterType;
                        }

                        Type declType = method.DeclaringType;
                        dm = new DynamicMethod("", invoke.ReturnType, argTypes, (declType.IsInterface || declType.IsArray) ? delegateType : declType, true);
                        break;
                    }
                }

                DynamicILInfo info = dm.GetDynamicILInfo();
                info.SetLocalSignature(new byte[] { 0x7, 0x0 });
                var code    = new byte[(2 + 5) * argTypes.Length + 6];
                int index   = 0;
                var mParams = method.GetParameters();
                int mIndex  = method.IsConstructor ? 0 : -1;
                for (int i = 0; i < argTypes.Length; i++)
                {
                    code[index++] = 0x0e;
                    code[index++] = (byte)i;

                    var mType = mIndex == -1 ? method.DeclaringType : mParams[mIndex].ParameterType;
                    if (mType.IsClass && !(mType.IsPointer || mType.IsByRef))
                    {
                        var cToken = info.GetTokenFor(mType.TypeHandle);
                        code[index++] = 0x74;
                        code[index++] = (byte)cToken;
                        code[index++] = (byte)(cToken >> 8);
                        code[index++] = (byte)(cToken >> 16);
                        code[index++] = (byte)(cToken >> 24);
                    }
                    else
                    {
                        index += 5;
                    }
                    mIndex++;
                }
                code[index++] = (byte)((byte)fieldInfo.Name[Mutation.KeyI8] ^ opKey);
                int dmToken = info.GetTokenFor(method.MethodHandle);
                code[index++] = (byte)dmToken;
                code[index++] = (byte)(dmToken >> 8);
                code[index++] = (byte)(dmToken >> 16);
                code[index++] = (byte)(dmToken >> 24);
                code[index]   = 0x2a;
                info.SetCode(code, argTypes.Length + 1);

                fieldInfo.SetValue(null, dm.CreateDelegate(delegateType));
            }
        }
コード例 #19
0
        public void Test_CallGM()
        {
            // GenericMethod inside GenericType
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(CallGM), typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            sigHelper.AddArgument(typeof(string), false);
            sigHelper.AddArgument(typeof(G <int>), false);
            sigHelper.AddArgument(typeof(G <string>), false);
            sigHelper.AddArgument(typeof(G <System.Type>), false);
            sigHelper.AddArgument(typeof(string), false);
            dynamicILInfo.SetLocalSignature(sigHelper.GetSignature());

            byte[] code =
            {
                0x00, 0x14, 0x0a, 0x73, 0x42, 0x00, 0x00, 0x0a, 0x0b, 0x06, 0x07, 0x1f, 0x64, 0x6f, 0x09, 0x00,
                0x00, 0x2b, 0x28, 0x2b, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x07, 0x72, 0x5f, 0x03, 0x00, 0x70, 0x6f,
                0x0a, 0x00, 0x00, 0x2b, 0x28, 0x2b, 0x00, 0x00, 0x0a, 0x0a, 0x73, 0x44, 0x00, 0x00, 0x0a, 0x0c,
                0x06, 0x08, 0x1f, 0x64, 0x6f, 0x0b, 0x00, 0x00, 0x2b, 0x28, 0x2b, 0x00, 0x00, 0x0a, 0x0a, 0x06,
                0x08, 0x72, 0x95, 0x02, 0x00, 0x70, 0x6f, 0x0c, 0x00, 0x00, 0x2b, 0x28, 0x2b, 0x00, 0x00, 0x0a,
                0x0a, 0x73, 0x46, 0x00, 0x00, 0x0a, 0x0d, 0x06, 0x09, 0x1f, 0x64, 0x6a, 0x6f, 0x0d, 0x00, 0x00,
                0x2b, 0x28, 0x2b, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x09, 0x73, 0x2c, 0x00, 0x00, 0x0a, 0x6f, 0x0e,
                0x00, 0x00, 0x2b, 0x28, 0x2b, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x13, 0x04, 0x2b, 0x00, 0x11, 0x04,
                0x2a
            };
            int token0  = dynamicILInfo.GetTokenFor(typeof(G <int>).GetConstructor(Type.EmptyTypes).MethodHandle, typeof(G <int>).TypeHandle);
            int token1  = dynamicILInfo.GetTokenFor(typeof(G <int>).GetMethod("M").MakeGenericMethod(typeof(int)).MethodHandle, typeof(G <int>).TypeHandle);
            int token2  = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }).MethodHandle);
            int token3  = dynamicILInfo.GetTokenFor("hello");
            int token4  = dynamicILInfo.GetTokenFor(typeof(G <int>).GetMethod("M").MakeGenericMethod(typeof(string)).MethodHandle, typeof(G <int>).TypeHandle);
            int token5  = dynamicILInfo.GetTokenFor(typeof(G <string>).GetConstructor(Type.EmptyTypes).MethodHandle, typeof(G <string>).TypeHandle);
            int token6  = dynamicILInfo.GetTokenFor(typeof(G <string>).GetMethod("M").MakeGenericMethod(typeof(int)).MethodHandle, typeof(G <string>).TypeHandle);
            int token7  = dynamicILInfo.GetTokenFor("world");
            int token8  = dynamicILInfo.GetTokenFor(typeof(G <string>).GetMethod("M").MakeGenericMethod(typeof(string)).MethodHandle, typeof(G <string>).TypeHandle);
            int token9  = dynamicILInfo.GetTokenFor(typeof(G <System.Type>).GetConstructor(Type.EmptyTypes).MethodHandle, typeof(G <System.Type>).TypeHandle);
            int token10 = dynamicILInfo.GetTokenFor(typeof(G <System.Type>).GetMethod("M").MakeGenericMethod(typeof(long)).MethodHandle, typeof(G <System.Type>).TypeHandle);
            int token11 = dynamicILInfo.GetTokenFor(typeof(System.Object).GetConstructor(Type.EmptyTypes).MethodHandle);
            int token12 = dynamicILInfo.GetTokenFor(typeof(G <System.Type>).GetMethod("M").MakeGenericMethod(typeof(System.Object)).MethodHandle, typeof(G <System.Type>).TypeHandle);

            PutInteger4(token0, 0x0004, code);
            PutInteger4(token1, 0x000e, code);
            PutInteger4(token2, 0x0013, code);
            PutInteger4(token3, 0x001b, code);
            PutInteger4(token4, 0x0020, code);
            PutInteger4(token2, 0x0025, code);
            PutInteger4(token5, 0x002b, code);
            PutInteger4(token6, 0x0035, code);
            PutInteger4(token2, 0x003a, code);
            PutInteger4(token7, 0x0042, code);
            PutInteger4(token8, 0x0047, code);
            PutInteger4(token2, 0x004c, code);
            PutInteger4(token9, 0x0052, code);
            PutInteger4(token10, 0x005d, code);
            PutInteger4(token2, 0x0062, code);
            PutInteger4(token11, 0x006a, code);
            PutInteger4(token12, 0x006f, code);
            PutInteger4(token2, 0x0074, code);
            dynamicILInfo.SetCode(code, 3);

            string ret = (string)dynamicMethod.Invoke(null, null);

            Assert.Equal(ret, CallGM());
        }
コード例 #20
0
        public void Test_GenericMethod()
        {
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(GenericMethod), typeof(bool), Type.EmptyTypes, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

            sigHelper.AddArgument(typeof(System.Int32[]), false);
            sigHelper.AddArgument(typeof(int), false);
            sigHelper.AddArgument(typeof(System.String[]), false);
            sigHelper.AddArgument(typeof(string), false);
            sigHelper.AddArgument(typeof(bool), false);
            sigHelper.AddArgument(typeof(System.String[]), false);
            dynamicILInfo.SetLocalSignature(sigHelper.GetSignature());

            byte[] code =
            {
                0x00, 0x1c, 0x8d, 0x1f, 0x00, 0x00, 0x01, 0x0a, 0x06, 0x16, 0x18, 0x9e, 0x06, 0x17, 0x1f, 0x09,
                0x9e, 0x06, 0x18, 0x15, 0x9e, 0x06, 0x19, 0x1f, 0x0e, 0x9e, 0x06, 0x1a, 0x19, 0x9e, 0x06, 0x1b,
                0x1f, 0x37, 0x9e, 0x06, 0x14, 0xfe, 0x06, 0x12, 0x00, 0x00, 0x06, 0x73, 0x3a, 0x00, 0x00, 0x0a,
                0x28, 0x06, 0x00, 0x00, 0x2b, 0x0b, 0x1b, 0x8d, 0x0e, 0x00, 0x00, 0x01, 0x13, 0x05, 0x11, 0x05,
                0x16, 0x72, 0x85, 0x02, 0x00, 0x70, 0xa2, 0x11, 0x05, 0x17, 0x72, 0x91, 0x02, 0x00, 0x70, 0xa2,
                0x11, 0x05, 0x18, 0x72, 0x95, 0x02, 0x00, 0x70, 0xa2, 0x11, 0x05, 0x19, 0x72, 0xa1, 0x02, 0x00,
                0x70, 0xa2, 0x11, 0x05, 0x1a, 0x72, 0xbd, 0x02, 0x00, 0x70, 0xa2, 0x11, 0x05, 0x0c, 0x08, 0x73,
                0x18, 0x00, 0x00, 0x06, 0xfe, 0x06, 0x13, 0x00, 0x00, 0x06, 0x73, 0x3b, 0x00, 0x00, 0x0a, 0x28,
                0x07, 0x00, 0x00, 0x2b, 0x0d, 0x07, 0x06, 0x19, 0x94, 0x33, 0x0b, 0x09, 0x08, 0x19, 0x9a, 0x28,
                0x3c, 0x00, 0x00, 0x0a, 0x2b, 0x01, 0x16, 0x13, 0x04, 0x2b, 0x00, 0x11, 0x04, 0x2a
            };
            int token0  = dynamicILInfo.GetTokenFor(typeof(int).TypeHandle);
            int token1  = dynamicILInfo.GetTokenFor(typeof(DynamicILInfoTests).GetMethod("MyRule", BindingFlags.NonPublic | BindingFlags.Static).MethodHandle);
            int token2  = dynamicILInfo.GetTokenFor(typeof(Satisfy <int>).GetConstructor(new Type[] { typeof(System.Object), typeof(System.IntPtr) }).MethodHandle, typeof(Satisfy <int>).TypeHandle);
            int token3  = dynamicILInfo.GetTokenFor(typeof(Finder).GetMethod("Find").MakeGenericMethod(typeof(int)).MethodHandle);
            int token4  = dynamicILInfo.GetTokenFor(typeof(string).TypeHandle);
            int token5  = dynamicILInfo.GetTokenFor("Hello");
            int token6  = dynamicILInfo.GetTokenFor("1");
            int token7  = dynamicILInfo.GetTokenFor("world");
            int token8  = dynamicILInfo.GetTokenFor("dynamicmethod");
            int token9  = dynamicILInfo.GetTokenFor("find it already");
            int token10 = dynamicILInfo.GetTokenFor(typeof(DynamicILInfoTests).GetConstructor(Type.EmptyTypes).MethodHandle);
            int token11 = dynamicILInfo.GetTokenFor(typeof(DynamicILInfoTests).GetMethod("MyRule", BindingFlags.NonPublic | BindingFlags.Instance).MethodHandle);
            int token12 = dynamicILInfo.GetTokenFor(typeof(Satisfy <string>).GetConstructor(new Type[] { typeof(System.Object), typeof(System.IntPtr) }).MethodHandle, typeof(Satisfy <string>).TypeHandle);
            int token13 = dynamicILInfo.GetTokenFor(typeof(Finder).GetMethod("Find").MakeGenericMethod(typeof(string)).MethodHandle);
            int token14 = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("op_Equality").MethodHandle);

            PutInteger4(token0, 0x0003, code);
            PutInteger4(token1, 0x0027, code);
            PutInteger4(token2, 0x002c, code);
            PutInteger4(token3, 0x0031, code);
            PutInteger4(token4, 0x0038, code);
            PutInteger4(token5, 0x0042, code);
            PutInteger4(token6, 0x004b, code);
            PutInteger4(token7, 0x0054, code);
            PutInteger4(token8, 0x005d, code);
            PutInteger4(token9, 0x0066, code);
            PutInteger4(token10, 0x0070, code);
            PutInteger4(token11, 0x0076, code);
            PutInteger4(token12, 0x007b, code);
            PutInteger4(token13, 0x0080, code);
            PutInteger4(token14, 0x0090, code);
            dynamicILInfo.SetCode(code, 4);

            bool ret = (bool)dynamicMethod.Invoke(null, null);

            Assert.Equal(ret, GenericMethod());
        }