我有一个相当大的项目,我尽量保持干净整洁。当我在 Visual Studio 中运行代码分析器时,我得到一个我觉得很烦人的可靠性错误。我真的很想学习如何解决它。这是我正在做的一个简化示例。
这是警告。
警告 1 CA2000:Microsoft.Reliability:在方法“MyExampleClassForStackOverflow.AddFeed(string)”中,在对对象“new FeedClassExamle()”的所有引用超出范围之前调用 System.IDisposable.Dispose。
这是我的示例代码:
class MyExampleClassForStackOverflow : IDisposable
{
    public ConcurrentDictionary<string, FeedClassExamle> Feeds { get; set; }
    public void AddFeed(string id)
    {
        //The warning is coming from this code block.
        //In the full code, the feed classes collects data on a specific 
        //interval and feeds them back using events.
        //I have a bunch of them and they need to be accessible so I 
        //store them in dictionaries using keys to effeciently find them.
        Feeds.TryAdd(id, new FeedClassExamle());
        Feeds[id].Start();
    }
    public void Dispose()
    {
        foreach (var item in Feeds)
            item.Value.Dispose();
    }
}
class FeedClassExamle : IDisposable
{
    public void Start()
    {
    }
    public void Dispose()
    {
    }
}
为了测试代码,请使用:
using (var example = new MyExampleClassForStackOverflow())
{
}
任何建议都会受到欢迎。