我正在制作一个声音合成程序,用户可以在其中创建自己的声音,进行基于节点的合成、创建振荡器、滤波器等。
该程序将节点编译为一种中间语言,然后通过 ILGenerator 和 DynamicMethod 将其转换为 MSIL。
它适用于存储所有操作和数据的数组,但如果我能够使用指针允许我稍后执行一些位级操作,它会更快。
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);