Why can't my view's model bind with my generic ViewModel which implements an interface? (ASP.NET MVC 3)

筅森魡賤 提交于 2019-12-01 17:50:04

What you're trying to accomplish should be possible by using an Interface for the ViewModel class and declaring the Generic type as Covariant. Like this:

interface ICompanyListViewModel<out T> where T : ICompany
{
}

That way you could also get away with an abstract class instead of an interface for Company.

Consider that interfaces cannot provide implementations when you think about this situation and it should become apparent that you need to close your generic type with the specific interface you want the generic class to operate upon.

If it's not apparent, well, consider further that from a pure OOP standpoint, interfaces can't define implementations, only classes can, and that C# doesn't allow you to specify overlapping interfaces.

It sounds to me like you are trying to treat an interface like an abstract base class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!