【问题标题】:VB.net anonymous class with inheritance具有继承的 VB.net 匿名类
【发布时间】:2015-05-27 22:01:45
【问题描述】:

想象一下我有这样的课:

Class A
    Public Function Some(str As String) As String
        Return "Some " + str
    End Function
End Class

我使用这样的代码:

Public Sub Foo()
    Dim thisWorks = New With {.prop = "thing"}
    Dim thisDoesntWork = New inherits A with { .prop = Some("thing") }
End Sub

我正在尝试创建一个具有继承的匿名类型,以便我可以使用其中的方法。这可能吗?

用例:我正在尝试创建一个具有 Select、From 等方法的类,这将有助于更清晰的查询构造。在消费代码中,我将创建一个从类继承的匿名类型并使用方法。

【问题讨论】:

标签: vb.net anonymous-types anonymous-class


【解决方案1】:

你想要的是不可能的(至少不是你描述的那样)。

根据我的理解;您应该尝试模仿 Linq 所做的事情;具有您想要的所有方法的接口(如 IEnumerable)(或者可能是阻止您从其他东西继承的抽象类 bu)+其他东西(如果您希望它们作为扩展方法,可能是模块)定义 Select 等作用于界面。

从那时起,您可以创建实现接口的类并在它们上使用您的自定义 Select 等

【讨论】:

    猜你喜欢
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多