Can';不要在Using语句中使用泛型C#类
我试图在Can';不要在Using语句中使用泛型C#类,c#,generics,idisposable,C#,Generics,Idisposable,我试图在using语句中使用泛型类,但编译器似乎无法将其视为实现IDisposable 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
using
语句中使用泛型类,但编译器似乎无法将其视为实现IDisposable
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>())
{
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Data.Object;
名称空间沙盒
{
公共密封类UnitOfWorkScope,其中T:ObjectContext,IDisposable,new()
{
公共空间处置()
{
}
}
公共类MyObjectContext:ObjectContext,IDisposable
{
public MyObjectContext():base(“DummyConnectionString”){}
#区域IDisposable成员
void IDisposable.Dispose()无效
{
抛出新的NotImplementedException();
}
#端区
}
公共类消费者
{
公共无效剂量测定法()
{
使用(新UnitOfWorkScope())
{
}
}
}
}
编译器错误为:
Error 1 'Sandbox.UnitOfWorkScope<Sandbox.MyObjectContext>': type used in a using statement must be implicitly convertible to 'System.IDisposable'
错误1“Sandbox.UnitOfWorkScope”:using语句中使用的类型必须隐式转换为“System.IDisposable”
我在UnitOfWorkScope上实现了IDisposable(并查看这是否是问题所在,在MyObjectContext上也是如此)
我错过了什么
我在UnitOfWorkScope上实现了IDisposable
不,你没有。您指定T应该实现IDisposable
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>())
{
}
}
}
}
使用以下语法:
public sealed class UnitOfWorkScope<T> : IDisposable where T : ObjectContext, IDisposable, new()
公共密封类UnitOfWorkScope:IDisposable其中T:ObjectContext,IDisposable,new()
因此,首先,声明UnitOfWorkScope实现的类/接口(IDisposable),然后声明T的约束(T必须派生自ObjectContext,实现IDisposable并具有无参数构造函数)您已经指定
UnitOfWorkScope
中的T
必须实现IDisposable
,但并不是说UnitOfWorkScope
本身实现了IDisposable
。我想你想要这个:
public sealed class UnitOfWorkScope<T> : IDisposable
where T : ObjectContext, IDisposable, new()
{
public void Dispose()
{
// I assume you'll want to call IDisposable on your T here...
}
}
公共密封类UnitOfWorkScope:IDisposable
其中T:ObjectContext,IDisposable,new()
{
公共空间处置()
{
//我想你会想在你的T上打电话给IDisposable。。。
}
}
除了需要在上实现IDisposable的对象之外,您已经在所有对象上实现了IDisposable:UnitOfWorkScope
实现了Dispose方法,但从未实现IDisposable。where子句适用于T,而不是类。+1-完全正确。UnitOfWorkScope未在给定源中实现IDisposable。