That's because generic types are invariant by default in c#. It means you cannot do following:
List<object> list = new List<string>();
or in your case
PortfolioUnitOfWork<IPortfolio> porfolio = new PortfolioUnitOfWork<Type1>();
And you can't do them variant, because in .NET only interface and delegate type parameters can be variant. Following error appears when you try:
Invalid variance modifier. Only interface and delegate type parameters can be specified as variant.
Possible way to go? Create IPortfolioUnitOfWork
:
public interface IPortfolioUnitOfWork
{ }
And change your PortfolioUnitOfWork<T>
to implement that:
public class PortfolioUnitOfWork<T> : IPortfolioUnitOfWork where T: class, IPortfolio
now you can do following:
IPortfolioUnitOfWork porfolio = new PortfolioUnitOfWork<Type1>();
ofc, you'll have to change your return statements and variable types from PortfolioUnitOfWork<IPortfolio>
to IPortfolioUnitOfWork
.