调用结构体的内部方法
我正在采取一种可怕的黑客手段来填充 XNA 框架中锁定的数据类型:结构中有一个内部方法,我希望在不向垃圾收集器提供数据的情况下调用该方法。
如果我将所述结构装箱在对象变量中并使用 MethodInfo.Invoke() ,该调用本身就会通过装箱参数来提供垃圾收集器:
private object boxedTouchCollection;
void test() {
MethodInfo addTouchLocationMethod = typeof(TouchCollection).GetMethod(
"AddTouchLocation", BindingFlags.Instance | BindingFlags.NonPublic
);
addTouchLocationMethod.Invoke(
this.boxedState, new object[] { /* parameters being boxed */ }
);
}
我不确定是否 Delegate.CreateDelegate ()
可以在这里使用 - 我可以将第一个参数转换为一个对象并且它可以在盒装结构上工作吗?或者我可以存储未装箱的结构并将第一个参数声明为 ref TouchCollection 吗?
delegate void AddTouchLocationDelegate(
ref TouchCollection collection,
int id,
// ...more parameters...
);
private TouchCollection touchCollection;
void test() {
Delegate.CreateDelegate(
typeof(AddTouchLocationDelegate),
typeof(ref TouchCollection), // doesn't compile
addTouchLocationMethod
);
}
有没有办法让 Delegate.CreateDelegate()
工作? 或者我必须求助于动态 IL 生成?
I am resorting to a horrible hack in order to fill a locked-down data type in the XNA framework: there's an internal method in a structure that I wish to call without feeding the garbage collector.
If I keep said structure boxed in an object variable and use MethodInfo.Invoke()
, that call would itself feed the garbage collector by boxing the parameters:
private object boxedTouchCollection;
void test() {
MethodInfo addTouchLocationMethod = typeof(TouchCollection).GetMethod(
"AddTouchLocation", BindingFlags.Instance | BindingFlags.NonPublic
);
addTouchLocationMethod.Invoke(
this.boxedState, new object[] { /* parameters being boxed */ }
);
}
I'm not sure whether Delegate.CreateDelegate()
can be used here - can I just turn the first parameter into an object and it will work on the boxed structure? Or can I store my structure unboxed and declare the first parameter as ref TouchCollection
?
delegate void AddTouchLocationDelegate(
ref TouchCollection collection,
int id,
// ...more parameters...
);
private TouchCollection touchCollection;
void test() {
Delegate.CreateDelegate(
typeof(AddTouchLocationDelegate),
typeof(ref TouchCollection), // doesn't compile
addTouchLocationMethod
);
}
Is there a way I can make Delegate.CreateDelegate()
work?
Or will I have to resort to dynamic IL generation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种方法。
它依赖于
Delegate.CreateDelegate
此重载 >,它创建开放实例方法委托。唯一棘手的一点是,您必须创建适当的委托类型才能通过引用传递结构。我认为这种技术不应该有任何装箱——无论是使用方法的参数,还是使用结构本身。
示例:(为简化示例类型而道歉)
Here's one way.
It relies on this overload of
Delegate.CreateDelegate
, which creates open instance-method delegates. The only tricky bit is that so you'll have to create the appropriate delegate-type to be able to pass the struct by reference.I don't think there should be any boxing with this technique - either with the arguments to the method, or with the struct itself.
Example: (Apologies for simplifying the example-types)
这是我同时发现的另一个使用 Linq 表达式树的解决方案:
用法很简单:
Here's another solution using Linq Expression Trees that I found in the meantime:
Usage is straightforward: