I believe it's because IX
is an interface. The compiler thinks that maybe a value of type IX
could already be derived from Wrapped<IX>
(even if Wrapped<T>
is sealed) so it doesn't use the conversion.
The details are quite complicated, in sections 6.4.3 and 6.4.4 of the C# 3.0 spec. Basically because IX
is an interface, it's not "encompassed by" any types, which means a later step in 6.4.4 fails.
I suggest you create a non-generic type Wrapped
with this method:
public static Wrapped<T> Of<T>(T item)
{
return new Wrapped<T>(item);
}
Then you can just write:
using (Wrapped<IX> wrappedIX = Wrapped.Of(plainIX))
Basically conversions can be a bit tricky for various reasons - simple methods are generally easier to understand, IMO.
Jon Skeet
2010-03-26 19:27:42