Can't Use Generic C# Class in Using Statement
Posted
by Eric J.
on Stack Overflow
See other posts from Stack Overflow
or by Eric J.
Published on 2010-06-03T05:39:59Z
Indexed on
2010/06/03
5:44 UTC
Read the original article
Hit count: 283
I'm trying to use a generic class in a using
statement but the compiler can't seem to treat it as implementing 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>())
{
}
}
}
}
Compiler error is:
Error 1 'Sandbox.UnitOfWorkScope<Sandbox.MyObjectContext>': type used in a using statement must be implicitly convertible to 'System.IDisposable'
I implemented IDisposable on UnitOfWorkScope (and to see if that was the problem, also on MyObjectContext).
What am I missing?
© Stack Overflow or respective owner