简体   繁体   English

警告CA1001实现IDisposable可忽略不计

[英]Is warning CA1001 implement IDisposable negligible

I am using Repository pattern and having my general repository class 我正在使用Repository模式并拥有我的通用存储库类

public class GeneralRepository<T> where T:class
{
    private Context con = new Context();
    protected DbSet<T> Dbset { get; set; }

    public GeneralRepository()
    {
        Dbset = con.Set<T>();
    }
    public IEnumerable<T> GetAll()
    {
        return Dbset;
    }
    public T SelectByID(int? id)
    {
        var rec = Dbset.Find(id);
        return rec;
    }
    public void AddRecord(T entity)
    {
        Dbset.Add(entity);
    }
    public void EditRecord(T entity)
    {
        con.Entry(entity).State = EntityState.Modified;
    }

    public void Detach(T entity)
    {
       con.Entry(entity).State = EntityState.Detached;
    }
    public void Save()
    {
        con.SaveChanges();
    }
}

There are multiple native repositories which are inheriting my General repository like: 有多个本机存储库继承我的General存储库,如:

public class EmailAssignRepository:GeneralRepository<EmailAssign>
public class DepartmentRepository:GeneralRepository<Department>

Everything is running fine in my code , but it gave me warning when I run code analysis. 我的代码中的一切运行正常,但是当我运行代码分析时它给了我警告。 What does it mean? 这是什么意思? is it negligible? 它可以忽略不计吗? If not how to overcome this warning? 如果不是如何克服这个警告?

Based on MSDN : 基于MSDN

A class implements the IDisposable interface to dispose of unmanaged resources that it owns. 类实现IDisposable接口以处置它拥有的非托管资源。 An instance field that is an IDisposable type indicates that the field owns an unmanaged resource. IDisposable类型的实例字段表示该字段拥有非托管资源。 A class that declares an IDisposable field indirectly owns an unmanaged resource and should implement the IDisposable interface. 声明IDisposable字段的类间接拥有非托管资源,并应实现IDisposable接口。 If the class does not directly own any unmanaged resources, it should not implement a finalizer. 如果类不直接拥有任何非托管资源,则不应实现终结器。

Cause: 原因:

A class declares and implements an instance field that is a System.IDisposable type and the class does not implement IDisposable . 类声明并实现一个System.IDisposable类型的实例字段,该类不实现IDisposable

How to Fix Violations 如何修复违规行为

To fix a violation of this rule, implement IDisposable and from the System.IDisposable.Dispose method call the Dispose method of the field. 要修复违反此规则的问题,请实现IDisposable并从System.IDisposable.Dispose方法调用该字段的Dispose方法。

And see this: How to implement IDisposable inheritance in a repository? 并看到这个: 如何在存储库中实现IDisposable继承?


When to Suppress Warnings (and your question is it negligible ? No) 什么时候禁止警告(你的问题可以忽略不计?不)

Do not suppress a warning from this rule. 请勿禁止此规则发出警告。

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

相关问题 CA1001在异步方法上实现IDisposable - CA1001 implement IDisposable on async method 内存异常; NullReference; CA1001实现IDisposable - Memory Exception; NullReference; CA1001 implement IDisposable 是CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable有效吗? - Is CA1001: TypesThatOwnDisposableFieldsShouldBeDisposable Valid? 对不使用 IDisposable 的框架组件使用 .NET 分析器规则 CA1001 - Using .NET analyzer rule CA1001 for components of a framework which does not use IDisposable CA1001 Visual Studio 2012代码分析警告。 这是什么意思? - CA1001 Visual Studio 2012 Code Analysis warning. What does it mean? CA1001报告了扩展类中的静态方法 - CA1001 reported on static method in extension class WPF代码分析:CA1001具有一次性字段的类型应该是一次性的 - WPF code analyze : CA1001 Types that own disposable fields should be disposable 错误消息:CA1001 - 错误消息:表单创建 IDiposable 类型 - Error Message: CA1001 - Error Message: Form Creates IDiposable Types 在没有CA警告的情况下实现已经过编程的IDisposable模式 - Implement the inhertited IDisposable pattern with no CA warnings 代码分析警告CA2213 - 在IDisposable支持字段上调用Dispose() - Code Analysis Warning CA2213 - Call Dispose() on IDisposable backing field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM