我在类里面有这个方法:
member this.GetDbSet<'TEntity, 'TDTO, 'TKey when 'TKey :> IEquatable<'TKey> and 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TDTO :> IDTO<'TKey> and 'TEntity : equality and 'TEntity : null and 'TDTO : equality and 'TDTO : null and 'TKey : equality>(repository : BaseRepository<'TEntity, 'TDTO, 'TKey>) =
repository.DbSetFuncGetter().Invoke(uow.Context())
但是当我构建项目时出现这个错误
This code is not sufficiently generic. The type variable 'TEntity when 'TEntity :> IEntity<'TKey> and 'TEntity : not struct and 'TEntity : equality and 'TEntity : null and 'TKey :> IEquatable<'TKey> and 'TKey : equality could not be generalized because it would escape its scope.
但是所有限制都在那里。我错过了什么吗?
编辑:
如果您需要更多代码:
这是开源项目
最佳答案
我不知道为什么会为我的代码显示该错误消息,但我找到了解决方法并提交了 GitHub 项目。您可以检查此文件的差异,因为它更容易阅读而不是描述它。
链接包含解决问题的差异,但我将在下几行中解释。
我用变通方法解决了它,我不知道它为什么会显示。
在存储库类(问题中存储库的类,不是方法的类)上有这些成员:
let mutable dbSetFunc : Func<DbContext, DbSet<'TEntity>> = null
member this.DbSetFunc
with set (value) = dbSetFunc <- value
member this.DbSetFuncGetter() = dbSetFunc
但是他们产生了错误,我发现它试图评论他们,我把他们改成了这些
[<DefaultValue>] val mutable dbSetFunc : Func<DbContext, DbSet<'TEntity>>
member this.DbSetFunc
with set (value) = this.dbSetFunc <- value
member internal this.DbSetFuncGetter() = this.dbSetFunc
而且我无法解释为什么这可以解决错误,至少我希望这会有所帮助。
问题中的方法保持不变。
关于.net - F# 泛型约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24430515/