我正在制作一个声音合成程序,用户可以在其中创建自己的声音,进行基于节点的合成,创建振荡器,滤波器等。
该程序将节点编译为中间语言,然后通过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);发布于 2011-11-25 17:58:42
如果您将正在执行的程序集的ManifestModule作为第四个参数传递给DynamicMethod构造函数,它将按预期工作:
DynamicMethod M = new DynamicMethod(
"HiThere",
ReturnType, Args,
Assembly.GetExecutingAssembly().ManifestModule);(来源:http://srstrong.blogspot.com/2008/09/unsafe-code-without-unsafe-keyword.html)
https://stackoverflow.com/questions/8263146
复制相似问题