我正在制作一个声音合成程序,用户可以在其中创建自己的声音,进行基于节点的合成、创建振荡器、滤波器等。
该程序将节点编译为中间语言,然后通过 ILGenerator 和动态方法将其转换为 EMAIL。
它使用一个存储所有操作和数据的数组,但如果我能够使用指针来允许我稍后执行一些位级操作,那么速度会更快。
PD:速度非常重要!
我注意到一个 DynamicMethod 构造函数重写有一个方法属性,其中一个是 UnsafeExport,但我无法使用它,因为唯一有效的组合是 Public+Static
这就是我正在尝试做的事情,这会引发 VerificationException:(只是为指针分配一个值)
//Testing delegate
unsafe delegate float TestDelegate(float* data);
//Inside the test method (which is marked as unsafe)
Type ReturnType = typeof(float);
Type[] Args = new Type[] { typeof(float*) };
//Can't use UnamangedExport as method attribute:
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);
如果您通过执行程序集的ManifestModule
作为第四个参数DynamicMethod
构造函数,它按预期工作:
DynamicMethod M = new DynamicMethod(
"HiThere",
ReturnType, Args,
Assembly.GetExecutingAssembly().ManifestModule);
(信用:http://srstrong.blogspot.com/2008/09/unsafe-code-without-unsafe-keyword.html http://srstrong.blogspot.com/2008/09/unsafe-code-without-unsafe-keyword.html)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)