的ILGenerator:如何使用非托管的指针? (我得到一个VerificationException) [英] ILGenerator: How to use unmanaged pointers? (I get a VerificationException)

查看:546
本文介绍了的ILGenerator:如何使用非托管的指针? (我得到一个VerificationException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个声音合成程序至极,用户可以创建自己的声音做节点基合成,创造振荡器,滤波器等...



所述程序编译节点到中间语言至极然后经由的ILGenerator和DynamicMethod的



转换到一个MSIL

它与在至极阵列的所有操作和数据存储,但它会更快,如果我能够使用指针,让我以后做一些位级操作



PD:速度是非常重要的!



我注意到一个DynamicMethod的构造器覆盖有一个方法属性附加伤害,至极一个是UnsafeExport,但我不能使用它,因为只有有效的组合是公共静态+



这是我想要做的至极抛出我VerificationException:(只是分配一个值的指针)

  //测试委托
不安全委托浮动TestDelegate(浮点*数据);

//里面的测试方法(至极被标记为不安全的)

类型的返回类型= typeof运算(浮点);
类型[] =参数数量新型[] {typeof运算(浮点*)};

//不能使用UnamangedExport的方法属性附加伤害:
DynamicMethod的M =新的DynamicMethod的(
HiThere,
的返回类型,参数数量);

的ILGenerator根= M.GetILGenerator();

//设置指针值至7.0:
Gen.Emit(OpCodes.Ldarg_0);
Gen.Emit(OpCodes.Ldc_R4,7F);
Gen.Emit(OpCodes.Stind_R4);

//只返回一个虚拟值:
Gen.Emit(OpCodes.Ldc_R4,20F);
Gen.Emit(OpCodes.Ret);



VAR德尔=(TestDelegate)M.CreateDelegate(typeof运算(TestDelegate));

浮动*数据=(浮点*)Marshal.AllocHGlobal(4);
// VerificationException抛出此:
float结果=德尔(数据);


解决方案

如果您通过执行程序集的 ManifestModule 作为对 DynamicMethod的构造函数中的第4个参数,它的工作原理像预期一样:

  DynamicMethod的M =新的DynamicMethod的(
HiThere,
的返回类型,参数数量,
Assembly.GetExecutingAssembly()ManifestModule);



(来源:的http://srstrong.blogspot.com/2008/09/unsafe-code-without-unsafe-keyword.html


I'm making a sound synthesis program in wich the user can create his own sounds doing node-base compositing, creating oscillators, filters, etc...

The program compiles the nodes onto an intermediary language wich is then converted onto an MSIL via the ILGenerator and DynamicMethod

It works with an array in wich all operations and data are stored, but it will be faster if i was able to use pointers to allow me to do some bit-level operations later

PD: Speed is very important!!

I noticed that one DynamicMethod contructor override has a method atribute, wich one is UnsafeExport, but i can't use it, because the only valid combination is Public+Static

This is what i'm trying to do wich throws me a VerificationException: (Just to assign a value to a pointer)

//Testing delegate
unsafe delegate float TestDelegate(float* data);

//Inside the test method (wich is marked as unsafe) 

        Type ReturnType = typeof(float);
        Type[] Args = new Type[] { typeof(float*) };

        //Can't use UnamangedExport as method atribute:
        DynamicMethod M = new DynamicMethod(
            "HiThere",
            ReturnType, Args);

        ILGenerator Gen = M.GetILGenerator();

        //Set the pointer value to 7.0:
        Gen.Emit(OpCodes.Ldarg_0);
        Gen.Emit(OpCodes.Ldc_R4, 7F);
        Gen.Emit(OpCodes.Stind_R4);

        //Just return a dummy value:
        Gen.Emit(OpCodes.Ldc_R4, 20F);
        Gen.Emit(OpCodes.Ret);



        var del = (TestDelegate)M.CreateDelegate(typeof(TestDelegate));

        float* data = (float*)Marshal.AllocHGlobal(4);
        //VerificationException thrown here:
        float result = del(data);

解决方案

If you pass the executing assembly's ManifestModule as the 4th parameter to the DynamicMethod constructor, it works as intended:

DynamicMethod M = new DynamicMethod(
    "HiThere",
    ReturnType, Args,
    Assembly.GetExecutingAssembly().ManifestModule);

(Credit: http://srstrong.blogspot.com/2008/09/unsafe-code-without-unsafe-keyword.html)

这篇关于的ILGenerator:如何使用非托管的指针? (我得到一个VerificationException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆