using covariance on an interface …Runtime Error Unable to cast object of type 'derviedAService ' to type 'iBaseService`1[mybase]'

前端 未结 1 661
死守一世寂寞
死守一世寂寞 2021-01-28 20:05
using System; 
using System.Collections.Generic;



namespace mymodels
{
public abstract class mybase
{
     public string v1 {get; set;}
}     


public class derivedA          


        
相关标签:
1条回答
  • 2021-01-28 20:28

    Your interface isn't covariant - it needs to be explicitly covariant when you declare it. So this:

    interface iBaseService<T>  where T : mymodels.mybase
    

    should be:

    interface iBaseService<out T>  where T : mymodels.mybase
    

    ... except that you should also follow .NET naming conventions, using PascalCase for namespace, type and method names. Your code is very unconventional at the moment.

    0 讨论(0)
提交回复
热议问题