Access to disposed closure - mark methods as safe

前端 未结 1 1175
栀梦
栀梦 2021-01-01 20:54

This is about ReSharper\'s warning \"Access to disposed closure\" which usually appears when an object which is later disposed is used in a lambda. Access to disposed closur

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-01 21:34

    You can use ReSharper's annotations to fix this. ReSharper has no way of knowing how long the closure will last, e.g. it might be assigned to a field, and so it warns you that you might possibly be using something that will be disposed by the time the lambda is called.

    You can fix it like this:

    void DoThisTwice([InstantHandle] Action action)
    {
        action();
        action();
    }
    

    The InstantHandle attribute tells ReSharper that the action is called immediately and not stored beyond the scope of the method.

    0 讨论(0)
提交回复
热议问题