简体   繁体   English

访问委托中的修改后的闭包

[英]Access to modified closure in delegate

I'm working with TaskCompletionSource. 我正在使用TaskCompletionSource。 I register event on object, but when I try to unregister in my event method, resharper underlines it with note: Access to modified closure. 我在对象上注册了事件,但是当我尝试在事件方法中取消注册时,resharper会用以下符号强调该事件:访问修改后的闭包。

here is my code 这是我的代码

var taskCompletionSource = new TaskCompletionSource<bool>();
OnConnectionStateChangedInd handlerConnectionStateChangedInd = null;
OnBootCompletedCnfCallback handlerBootCompletedCnfCallback = null;

handlerConnectionStateChangedInd = (id, method, addr, port, nick) =>
{
    _corePttObject.onConnectionStateChangedInd -= handlerConnectionStateChangedInd;
    _connectionState = id;

    taskCompletionSource.SetResult(true);
};
_corePttObject.onConnectionStateChangedInd += handlerConnectionStateChangedInd;

This line is underlined: 该行带有下划线:

_corePttObject.onConnectionStateChangedInd -= handlerConnectionStateChangedInd; _corePttObject.onConnectionStateChangedInd-= handlerConnectionStateChangedInd;

Here is my complete definition of method: 这是我对方法的完整定义:

public Task<LoginResult> LoginAsync(string address)
    {
        var taskCompletionSource = new TaskCompletionSource<LoginResult>();
        OnUserAcceptCertWithNamePasswInd handlerAcceptCertWithNamePasswInd = null;
        OnAppExLoginProtocolServiceCnf handlerAppExLoginProtocolServiceCnf = null;
        handlerAcceptCertWithNamePasswInd = (cert, caCert, rootCert, hash, pos, data) =>
        {
            var loginCompletedArgs = new LoginResult
            {
                SvrCertificate = ParseCertificate(cert),
                CaCertificate = ParseCertificate(caCert),
                RootCertificate = ParseCertificate(rootCert),
                CertificateHash = hash,
                GridPosition = pos,
                GridData = data
            };
            _corePttObject.onUserAcceptCertWithNamePasswInd -= handlerAcceptCertWithNamePasswInd;
            taskCompletionSource.SetResult(loginCompletedArgs);
        };

        handlerAppExLoginProtocolServiceCnf = (nick, result, cause, link) =>
        {
            _corePttObject.onAppExLoginProtocolServiceCnf -= handlerAppExLoginProtocolServiceCnf;
        };

        _corePttObject.onAppExLoginProtocolServiceCnf += handlerAppExLoginProtocolServiceCnf;
        _corePttObject.onUserAcceptCertWithNamePasswInd += handlerAcceptCertWithNamePasswInd;
        //TODO: read id.
        _corePttObject.Login(address, true, "ID");

        return taskCompletionSource.Task;
    }

If you click click on the lightbulb suggestion, there will be an option "Why is ReSharper suggesting this". 如果您单击灯泡建议,将有一个选项“为什么ReSharper建议这样做”。 If you click on it, it will take you to this useful explanation . 如果单击它,将带您到此有用的说明

I'd need more context to tell if this potential trap can actually hurt you in your particular case. 我需要更多的背景信息来说明这种潜在陷阱是否确实会在您的特定情况下伤害您。

You can also check out this answer here . 您也可以在这里查看此答案

The reason for this warning is: for handlerConnectionStateChangedInd you declare an anonymous method that - when eventually executed - will access/change _corePttObject.onConnectionStateChangedInd . 产生此警告的原因是:对于handlerConnectionStateChangedInd您声明了一个匿名方法,该方法最终执行时将访问/更改_corePttObject.onConnectionStateChangedInd

Then, after this declaration, you change _corePttObject.onConnectionStateChangedInd already in this line: 然后, 此声明之后,您已经在此行中更改了 _corePttObject.onConnectionStateChangedInd

_corePttObject.onConnectionStateChangedInd += handlerConnectionStateChangedInd;

So ReSharper is warning you that the value _corePttObject.onConnectionStateChangedInd is different between the time you declare handlerConnectionStateChangedInd and the time it is executed. 因此,ReSharper向您警告,在声明handlerConnectionStateChangedInd的时间与执行该时间之间, _corePttObject.onConnectionStateChangedInd值会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM