继承类型之间的奇怪VB转换

时间:2009-10-14 14:22:36

标签: vb.net casting moq

我有一个基类定义为:

Public MustInherit Class BaseRepository(Of T As Class)

和一个继承自此类的类,定义为:

    Public Class CommunicationTypeRepository
        Inherits BaseRepository(Of CommunicationType)

出于测试目的,我创建了一个帮助方法,它将使用Moq来帮助获取一些基本数据:

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

GetAll方法位于BaseRepository类。

在运行时我收到错误:

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

我错过的显而易见的事情是什么?

哦,这是错误的callstack,让我觉得转换是在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.CommunicationTypeRepositoryTest.Correct_Item_Found_When_Searched_By_Code()

0 个答案:

没有答案