Define an abstract class that inherits an infterface, but does not implement it

前端 未结 2 378
执念已碎
执念已碎 2021-01-12 12:29

incorporating leppies feedback it compiles - but IMO some drawbacks I want each sub class to be forced by the compiler to define their own Uri property. Code as it is now:

相关标签:
2条回答
  • 2021-01-12 13:09

    Here is a way to do it:

    type IUriProvider =  
        abstract member UriString: string
        abstract member Uri : System.Uri
    
    [<AbstractClass>]  
    type UriUserControl() as this =  
        inherit System.Windows.Controls.UserControl() 
        abstract member Uri : System.Uri
        abstract member UriString : string
        interface IUriProvider with 
            member x.Uri = this.Uri
            member x.UriString = this.UriString
    

    Note that you have to provide an implementation of the interface (since all interface implementations in F# are explicit), but this can just refer back to abstract members in the class. Then you can subclass thusly:

    type ConcreteUriUserControl() =
        inherit UriUserControl()
        override this.Uri = null
        override this.UriString = "foo"
    
    0 讨论(0)
  • 2021-01-12 13:10

    From a .NET point of view, you would need to at least provide an abstract implementation for the interface. But that again could proof problematic due to default interface accessibility, which would require some more glue again for an explicit implementation.

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