Ejemplo n.º 1
0
        public void ReadDynamicMethod()
        {
            var module = ModuleDefinition.FromFile(typeof(TDynamicMethod).Assembly.Location);

            var type = module.TopLevelTypes.First(t => t.Name == nameof(TDynamicMethod));

            var method = type.Methods.FirstOrDefault(m => m.Name == nameof(TDynamicMethod.GenerateDynamicMethod));

            DynamicMethod generateDynamicMethod = TDynamicMethod.GenerateDynamicMethod();

            //Dynamic method => CilMethodBody
            var body = CilMethodBody.FromDynamicMethod(method, generateDynamicMethod);

            Assert.NotNull(body);

            Assert.NotEmpty(body.Instructions);

            Assert.Equal(body.Instructions.Select(q => q.OpCode), new CilOpCode[]
            {
                CilOpCodes.Ldarg_0,
                CilOpCodes.Call,
                CilOpCodes.Ldarg_1,
                CilOpCodes.Ret
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a Dynamic Method Definition
        /// </summary>
        /// <param name="module">Target Module</param>
        /// <param name="dynamicMethodObj">Dynamic Method / Delegate / DynamicResolver</param>
        public DynamicMethodDefinition(ModuleDefinition module, object dynamicMethodObj) :
            base(new MetadataToken(TableIndex.Method, 0))
        {
            dynamicMethodObj = DynamicMethodHelper.ResolveDynamicResolver(dynamicMethodObj);
            var methodBase = FieldReader.ReadField <MethodBase>(dynamicMethodObj, "m_method");

            Module        = module;
            Name          = methodBase.Name;
            Attributes    = (MethodAttributes)methodBase.Attributes;
            Signature     = new ReferenceImporter(module).ImportMethodSignature(ResolveSig(methodBase, module));
            CilMethodBody = CilMethodBody.FromDynamicMethod(this, dynamicMethodObj);
        }