【问题标题】:Access to modified closure in delegate在委托中访问修改后的闭包
【发布时间】:2016-05-31 18:13:42
【问题描述】:

我正在使用 TaskCompletionSource。我在对象上注册了事件,但是当我尝试在我的事件方法中取消注册时,resharper 会在它下面加注:访问修改后的闭包。

这是我的代码

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;

这条线有下划线:

_corePttObject.onConnectionStateChangedInd -= handlerConnectionStateChangedInd;

这是我对方法的完整定义:

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;
    }

【问题讨论】:

    标签: c# resharper


    【解决方案1】:

    此警告的原因是:对于 handlerConnectionStateChangedInd,您声明了一个匿名方法 - 当最终执行时 - 将访问/更改 _corePttObject.onConnectionStateChangedInd

    那么,这个声明之后,你更改 _corePttObject.onConnectionStateChangedInd已经在这一行了:

    _corePttObject.onConnectionStateChangedInd += handlerConnectionStateChangedInd;
    

    因此,ReSharper 警告您 _corePttObject.onConnectionStateChangedInd 的值在您声明 handlerConnectionStateChangedInd 的时间和执行它的时间之间是不同的。

    【讨论】:

    • 那么我应该担心什么问题吗?
    • @JurajP 只要您知道自己在做什么,就无需担心。你的代码看起来还可以。
    【解决方案2】:

    如果您单击灯泡建议,则会出现一个选项“为什么 ReSharper 建议这样做”。如果你点击它,它将带你到这个有用的explanation

    我需要更多的上下文来判断这个潜在的陷阱是否真的会在你的特定情况下伤害你。

    您也可以查看this answer here

    【讨论】:

      猜你喜欢
      • 2011-09-29
      • 2011-07-28
      • 2010-09-23
      • 2015-11-08
      • 1970-01-01
      • 2019-05-28
      • 2012-09-14
      • 1970-01-01
      相关资源
      最近更新 更多