This is similar to another topic I recently posted, but perhaps this might be simpler and clearer:
I want to accomplish the following (or something very similar)…
IManageableEntryDao<IManageableEntry> dao = new CompanyNameDao();
… with the following classes:
public interface IManageableEntry {
string Name { get; set; }
}
public class CompanyName : IManageableEntry {
public string Name { get; set; }
}
public interface IManageableEntryDao<T> where T : IManageableEntry {
}
public class CompanyNameDao : IManageableEntryDao<CompanyName> {
}
If I try to do a cast as IManageableEntryDao<IManageableEntry>, I get a null.
See Variance in Generic Interfaces. Change the interface to
IManageableEntryDao<out T>and it should work (unless the interface uses it in a way which makes this invalid).