不知道我在做什么错:
class MyClass
{
private EventInfo eventInfo;
public void OnGenerateEvent(object sender, EventArgs e)
{
// Called from *main* thread
// Load assembly and set eventInfo here
eventInfo = ....GetEvent(...);
eventInfo.AddEventHandler(source, handler);
// Call to a static method in another assembly
someMethodInfo.Invoke(null, null);
}
public void OnEventChanged(object sender, EventArgs args)
{
// Called from a *worker* thread created
// by that static method in the other assembly
eventInfo is null here !
// Trying to remove handler
eventInfo.RemoveEventHandler(.....);
}
// But...
protected override void Dispose(bool disposing)
{
// Called from *main* thread when program closes
eventInfo is *not* null here
}
}
最佳答案
我认为我们需要查看可重现的代码,但是我可以看到4种情况:
MyClass
实例交谈-我的赌注在这里 eventInfo
的变量(如果您有任何歧义,请在表示字段时尝试使用this.eventInfo
)volatile
;再次不太可能)前两个可能性更大。