简体   繁体   English

Reflection.Emit 不安全代码

[英]Reflection.Emit unsafe code

I'm doing some hacking with Reflection Emit in F#.我正在使用 F# 中的 Reflection Emit 进行一些黑客攻击。 I'm trying to do the equivalent of this C# code:我正在尝试执行与此 C# 代码等效的操作:

var ass = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.RunAndSave);
var mb = ass.DefineDynamicModule("Test", "Test.dll", false);
var t = mb.DefineType("Foo", TypeAttributes.Public, typeof(ValueType));
t.DefineField("foo", typeof(int*), FieldAttributes.Public);
t.CreateType();
ass.Save("Test.dll");

The line in particular is the definefield line.特别是该线是definefield线。 In F# the closest I can get is在 F# 中,我能得到的最接近的是

let f = t.DefineField("foo", (typeof<nativeptr<int>>), FieldAttributes.Public)

However the compiled field ends up compiled to of type IntPtr instead of int*.(Note its an IntPtr for nativeint or any other type too)然而,编译字段最终被编译为 IntPtr 类型而不是 int*。(注意它也是 nativeint 或任何其他类型的 IntPtr)

Additionally, I'd like to be able to use the struct I defined defined programatically and have a Foo* in another struct, but obviously I'm not able to use typeof in that case (in either C# or F#).此外,我希望能够使用我以编程方式定义的结构体,并在另一个结构体中有一个 Foo*,但显然我不能在这种情况下使用 typeof(在 C# 或 F# 中)。 Is there a way to define a field as Foo* from the Type/TypeBuilder?有没有办法从 Type/TypeBuilder 中将字段定义为 Foo*?

Before anyone points out that this is all potentially insane (and I agree), it is for a fun project for myself to figure out how crazy I can generate code that C# mignt not let me :)在有人指出这一切都可能是疯狂的之前(我同意),这是一个有趣的项目,让我自己弄清楚我可以生成 C# 不允许我的代码有多疯狂:)

I believe ilsigptr<int> is the F# counterpart to C#'s int* .我相信ilsigptr<int>是 C# 的int*的 F# 对应物。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM