For a class to be castable like this it would need to implement both interfaces IConstrained<decimal> and IConstrained<IComparable>
class A:IConstrained<decimal>,IConstrained<IComparable>
It does not happen automatically, because .NET 2.0 does not implement covariance or contravariance. IConstrained<decimal>
doesn't implement IConstrained<IComparable>
. Yes, it is a but frustrating and counter intuitive. There will actually be some support for this kind of scenario in C# 4.0 in one form or another from what I understand. It is called covariance or contravariance.
Edit: I'm not familier with the Constrained class, but you might be able to construct a new Contrained<IComparable>
and pass to it a decimal. If it had a constructor of the form Constrained<T> (T copyFrom)
then you can declare a new Constrained<IComparable>
and pass the decimal to it. Kind of like making a copy.
Edit 2: About half way down this page, search for "2.0", there is an example of how to work around this issue in .NET 2.0:
http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance