我需要实现IDataErrorInfo基类中的接口(interface)。该接口(interface)需要一个属性和一个索引器。我想为两者提供默认实现并允许子类覆盖它。我似乎无法使用接口(interface)实现的语法来使用“虚拟”实现的语法!例如:
type ViewModelBase() =
interface IDataErrorInfo with
abstract Error : string with get
default this.Error with get() = ""
出现以下编译错误
Error 1 Unexpected keyword 'abstract' in member definition. Expected 'member', 'override' or other token. D:\MinorApps\VetCompass\VetCompass\ViewModel\ViewModelBase.fs 18 7 VetCompass
Error 2 Incomplete structured construct at or before this point in pattern D:\MinorApps\VetCompass\VetCompass\ViewModel\ViewModelBase.fs 19 7 VetCompass
我什至不知道从哪里开始使用索引器!
最佳答案
所有接口(interface)实现都是显式的,这意味着当被视为类的成员时,接口(interface)的方法将是私有(private)的。因此,您不能在实现中使用 abstract
和 default
修饰符。相反,您需要添加一些重复内容:
type ViewModelBase() =
// declare a new virtual property
abstract Error : string
default this.Error = ""
interface IDataErrorInfo with
// pass through to the virtual property implementation
member this.Error = this.Error
关于用于实现 'virtual' 属性(实现接口(interface))的 F# 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860712/