爲了在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一代?
非常感謝!我沒有嘗試過載。偉大的工作,零垃圾。 – Cygon 2010-11-19 19:38:06