原因是:“ GC 收集了.NET 对象,该对象从 Outlook 中包装了 COM 对象”)。解决方案是对此.NET 对象的保留引用。最简便的方法是:
// this is helper collection.
// there are all wrapper objects
// , which should not be collected by GC
private List<object> holdedObjects = new List<object>();
// hooks necesary events
void HookEvents() {
// finds button in commandbars
CommandBarButton btnSomeButton = FindCommandBarButton( "MyButton ");
// hooks "Click" event
btnSomeButton.Click += btnSomeButton_Click;
// add "btnSomeButton" object to collection and
// and prevent themfrom collecting by GC
holdedObjects.Add( btnSomeButton );
}
1 回答
原因是:“ GC 收集了.NET 对象,该对象从 Outlook 中包装了 COM 对象”)。解决方案是对此.NET 对象的保留引用。最简便的方法是:
如果需要,您还可以为此(和其他)具体按钮(或其他对象)有一个特殊字段。但这是最常见的解决方案。