【问题标题】:Cannot understand why getting "type used in a using statement must be implicitly convertible to 'System.IDisposable'" error无法理解为什么获取“在 using 语句中使用的类型必须隐式转换为 'System.IDisposable'”错误
【发布时间】:2019-09-19 15:19:18
【问题描述】:

我正在使用 Visual Studio 2010、.NET Framework 4。

我得到 type used in a using statement must be implicitly convertible to 'System.IDisposable' 错误,尽管我正在实现 IDisposable 所以我不确定我在哪里犯了错误。请检查并让我知道。

我有一个名为MyDatabaseContext 的类,其中包含DbConnection;它实现了IDisposable

public abstract class MyDatabaseContext : IDisposable
{
   private string _dataProvider;
   private string _connectionString;
   private DbConnection _dbConnection;

   public MyDatabaseContext(string dataProvider, string connectionString)
   {
      _dataProvider = dataProvider;
      _connectionString = connectionString;
   }

   public void OpenConnection()
   {
      DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(_dataProvider);
      _dbConnection = dbProviderFactory.CreateConnection();
      _dbConnection.ConnectionString = _connectionString;
      _dbConnection.Open();
   }

   public void Dispose()
   {
      if (_dbConnection != null)
      {
         if (_dbConnection.State != ConnectionState.Closed)
         {
            _dbConnection.Close();
         }
         _dbConnection.Dispose();
         _dbConnection = null;
      }
   }
}

现在我有一个类EmployeeDatabaseContext,它继承自MyDatabaseContext。在其中我有一个方法 test() 我在 using 块内调用父类的 OpenConnection() 方法。

public class EmployeeDatabaseContext : MyDatabaseContext
{
   public EmployeeDatabaseContext(string dataProvider, string connectionString)
            : base(dataProvider, connectionString)
   {
   }

   public void test()
   {
      using (OpenConnection())
      {
      }
   }
}

问题是我在构建时遇到错误。错误出现在 using 块的 test() 方法中的 EmployeeDatabaseContext 类中。错误是

'void':在 using 语句中使用的类型必须是可隐式转换的 到“System.IDisposable”

但是父类 MyDatabaseContext 正在实现 IDisposable 所以我不知道为什么会出现这个错误。

谢谢

【问题讨论】:

  • OpenConnection() 返回无效。要在 using 语句中使用它,它需要返回一个实现 IDisposable 的对象。拥有该方法的类实现了什么并不重要。 using 语句不会去寻找附近的东西来处理。它会准确地处理你给它的东西,而你没有给它任何东西。

标签: c# visual-studio-2010


【解决方案1】:

OpenConnection() 返回无效。要在 using 语句中使用它,它需要返回一个实现 IDisposable 的对象。拥有该方法的类实现了什么并不重要。 using 语句不会去寻找附近的东西来处理。它会准确地处理你给它的东西,而你没有给它任何东西。

所以:

public MyDatabaseContext(string dataProvider, string connectionString)
{
    _dataProvider = dataProvider;
    _connectionString = connectionString;

    OpenConnection();
}

但不要公开OpenConnection(),因为您不希望闯入者重复调用它:

protected void OpenConnection()
{
    // ...
}

您在 EmployeeDatabaseContext 的构造函数中正确调用了基本构造函数,这很好。

下面是在 using 语句中使用它的方法:

public void test()
{
    using (var ctxt = new EmployeeDatabaseContext(someProviderString, someConnString))
    {
        //  Do stuff with ctxt
        //  ctxt.Dispose() will be called when control exits this block. 
    }
}

【讨论】:

  • 谢谢。我认为using 比它更聪明。现在我在想在父类中有 _dbConnection 有什么意义。
  • @srh 如果您有多个子类,每个子类都需要一个,那么在父类中声明一次是正确的。对于要使用它的子类,您可以通过公共或受保护的属性在基类中公开它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多