开发者

Strange VB casting between inherited types

I have a base class defined as:

Public MustInherit Class BaseRepository(Of T As Class)

and a class that inherits from this class, defined as:

    Public Class CommunicationTypeRepository
        Inherits BaseRepository(Of CommunicationType)

For testing purposes I have created a helper method that will use Moq to help fetching some basic data:

Public Shared Function GetRepository(Of TEntity As Class, _
                           TRepo As BaseRepository(Of TEntity)) _
                           (ByVal sampleData As IList(Of TEntity)) As TRepo

    Dim repoWithFakeBase = New Mock(Of TRepo)()
    repoWithFakeBase.Setup(Function(x) x.GetAll()).Returns(sampleData.AsQueryable())
    Return repoWithFakeBase.Object
End Function

The GetAll method is in the BaseRepository class.

At runtime I get the error:

System.InvalidOperationException : No coercion operator is defined between types 'DomainModel.Concrete.CommunicationTypeRepository' and 'DomainModel.Abstract.BaseRepository`1[T]'.

What's the obvious thing that I'm missing?

Oh and here's the callstack of the error, what supprised me was that the conversion is being done inside the linq namesapce:

System.InvalidOperationException : No coercion operator is defined between types 'DomainModel.Concrete.CommunicationTypeRepository' and 'DomainModel.Abstract.BaseRepository`1[T]'.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
at DomainTests.Concrete.CommunicationTypeRepositoryTest.GetRepository[TEntity,TRepo](IList`1 sampleData)
at DomainTests.Concrete.Commu开发者_Go百科nicationTypeRepositoryTest.Correct_Item_Found_When_Searched_By_Code()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜