Пример #1
0
        public void GetTokenFor_String_Success()
        {
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(HelloWorld), typeof(string), new Type[] { }, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

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

            byte[] code =
            {
                0x00, 0x72, 0x01, 0x00, 0x00, 0x70, 0x6f, 0x04, 0x00, 0x00, 0x0a, 0x0a, 0x2b, 0x00, 0x06, 0x2a
            };
            int token0 = dynamicILInfo.GetTokenFor("hello, world");
            int token1 = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("ToUpper", Type.EmptyTypes).MethodHandle);

            PutInteger4(token0, 0x0002, code);
            PutInteger4(token1, 0x0007, code);
            dynamicILInfo.SetCode(code, 1);

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

            Assert.Equal(ret, HelloWorld());
        }
Пример #2
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)
        {
            //create a dynamic method
            var dynamicMethod = new DynamicMethod("_" + Guid.NewGuid().ToString("N"), typeof(R), new[] { 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
            var 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
            var expressionDelegate = (ExecuteExpression <R, C>)dynamicMethod.CreateDelegate(typeof(ExecuteExpression <R, C>));

            return(expressionDelegate);
        }
Пример #3
0
        public void GetTokenFor_StringGenerics_Success()
        {
            DynamicMethod dynamicMethod = new DynamicMethod(nameof(ContactString), typeof(string), Type.EmptyTypes, typeof(DynamicILInfoTests), false);
            DynamicILInfo dynamicILInfo = dynamicMethod.GetDynamicILInfo();

            SignatureHelper sigHelper = SignatureHelper.GetLocalVarSigHelper();

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

            byte[] code =
            {
                0x00, 0x73, 0x26, 0x00, 0x00, 0x0a, 0x0a, 0x06, 0x72, 0x29, 0x01, 0x00, 0x70, 0x6f, 0x27, 0x00,
                0x00, 0x0a, 0x00, 0x06, 0x72, 0x37, 0x01, 0x00, 0x70, 0x6f, 0x27, 0x00, 0x00, 0x0a, 0x00, 0x7e,
                0x28, 0x00, 0x00, 0x0a, 0x0b, 0x00, 0x06, 0x6f, 0x29, 0x00, 0x00, 0x0a, 0x13, 0x04, 0x2b, 0x12,
                0x11, 0x04, 0x6f, 0x2a, 0x00, 0x00, 0x0a, 0x0c, 0x00, 0x07, 0x08, 0x28, 0x2b, 0x00, 0x00, 0x0a,
                0x0b, 0x00, 0x11, 0x04, 0x6f, 0x20, 0x00, 0x00, 0x0a, 0x13, 0x05, 0x11, 0x05, 0x2d, 0xe1, 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 <string>).GetConstructor(Type.EmptyTypes).MethodHandle, typeof(MyList <string>).TypeHandle);
            int token1 = dynamicILInfo.GetTokenFor("Hello~");
            int token2 = dynamicILInfo.GetTokenFor(typeof(MyList <string>).GetMethod("Add").MethodHandle, typeof(MyList <string>).TypeHandle);
            int token3 = dynamicILInfo.GetTokenFor("World!");
            int token4 = dynamicILInfo.GetTokenFor(typeof(string).GetField("Empty").FieldHandle);
            int token5 = dynamicILInfo.GetTokenFor(typeof(MyList <string>).GetMethod("GetEnumerator").MethodHandle, typeof(MyList <string>).TypeHandle);
            int token6 = dynamicILInfo.GetTokenFor(typeof(System.Collections.Generic.IEnumerator <string>).GetMethod("get_Current").MethodHandle, typeof(System.Collections.Generic.IEnumerator <string>).TypeHandle);
            int token7 = dynamicILInfo.GetTokenFor(typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }).MethodHandle);
            int token8 = dynamicILInfo.GetTokenFor(typeof(System.Collections.IEnumerator).GetMethod("MoveNext").MethodHandle);
            int token9 = dynamicILInfo.GetTokenFor(typeof(System.IDisposable).GetMethod("Dispose").MethodHandle);

            PutInteger4(token0, 0x0002, code);
            PutInteger4(token1, 0x0009, code);
            PutInteger4(token2, 0x000e, code);
            PutInteger4(token3, 0x0015, code);
            PutInteger4(token2, 0x001a, code);
            PutInteger4(token4, 0x0020, code);
            PutInteger4(token5, 0x0028, code);
            PutInteger4(token6, 0x0033, code);
            PutInteger4(token7, 0x003c, code);
            PutInteger4(token8, 0x0045, code);
            PutInteger4(token9, 0x005f, code);
            dynamicILInfo.SetCode(code, 2);
            byte[] exceptions =
            {
                0x41, 0x1c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x2e, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
                0x51, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
            };
            dynamicILInfo.SetExceptions(exceptions);

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

            Assert.Equal(ret, ContactString());
        }
Пример #4
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());
        }
Пример #5
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());
        }
Пример #6
0
        // Token: 0x060000FD RID: 253 RVA: 0x0000AA60 File Offset: 0x00008C60
        private static void c9caeb49525e24fdeaf8f5bc58177b94e(MethodBody cda702ac0b563445a1ac0a319f3ad44f9, DynamicILInfo c3c57c9acc0a08dbaa5fcc9490f2a3eac)
        {
            SignatureHelper localVarSigHelper          = SignatureHelper.GetLocalVarSigHelper();
            IEnumerator <LocalVariableInfo> enumerator = cda702ac0b563445a1ac0a319f3ad44f9.LocalVariables.GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    LocalVariableInfo localVariableInfo = enumerator.Current;
                    localVarSigHelper.AddArgument(localVariableInfo.LocalType, localVariableInfo.IsPinned);
                }
                for (;;)
                {
                    switch (4)
                    {
                    case 0:
                        continue;
                    }
                    break;
                }
                if (!true)
                {
                    RuntimeMethodHandle runtimeMethodHandle = methodof(c112201421a408a8f2963cee12a5d2e69.c9caeb49525e24fdeaf8f5bc58177b94e(MethodBody, DynamicILInfo)).MethodHandle;
                }
            }
            finally
            {
                if (enumerator != null)
                {
                    for (;;)
                    {
                        switch (5)
                        {
                        case 0:
                            continue;
                        }
                        break;
                    }
                    enumerator.Dispose();
                }
            }
            c3c57c9acc0a08dbaa5fcc9490f2a3eac.SetLocalSignature(localVarSigHelper.GetSignature());
        }
        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);
        }
Пример #8
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());
        }