我试图在using
语句中使用泛型类,但编译器似乎无法将其视为实现IDisposable。在使用语句中不能使用通用C#类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
namespace Sandbox
{
public sealed class UnitOfWorkScope<T> where T : ObjectContext, IDisposable, new()
{
public void Dispose()
{
}
}
public class MyObjectContext : ObjectContext, IDisposable
{
public MyObjectContext() : base("DummyConnectionString") { }
#region IDisposable Members
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
#endregion
}
public class Consumer
{
public void DoSomething()
{
using (new UnitOfWorkScope<MyObjectContext>())
{
}
}
}
}
编译器错误是:
Error 1 'Sandbox.UnitOfWorkScope<Sandbox.MyObjectContext>': type used in a using statement must be implicitly convertible to 'System.IDisposable'
我实现了IDisposable上UnitOfWorkScope(并看看是否这就是问题所在,也MyObjectContext)。
我错过了什么?
+1 - 确切地说。 UnitOfWorkScope不在给定的源中实现IDisposable。 – TomTom 2010-06-03 05:48:23