VB.net 匿名 class 继承
VB.net anonymous class with inheritance
假设我 class 是这样的:
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
我正在尝试创建一个具有继承性的匿名类型,以便我可以使用其中的方法。这可能吗?
用例:我正在尝试创建一个 class,它具有 Select、From 等方法,这将有助于更清晰的查询构造。在使用代码中,我只创建一个继承自 class 的匿名类型并使用这些方法。
你想要的是不可能的(至少不是你描述的那样)。
据我所知;您应该尝试模仿为 Linq 所做的事情;一个接口(如 IEnumerable),带有你想要的所有方法(或者可能是一个抽象的 class bu,阻止你从其他东西继承)+ 其他东西(如果你想要它们作为扩展方法,可能是一个模块)定义 Select等作用于界面
从那时起,您可以创建 class 实现接口并在其上使用您的自定义 Select 等
假设我 class 是这样的:
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
我正在尝试创建一个具有继承性的匿名类型,以便我可以使用其中的方法。这可能吗?
用例:我正在尝试创建一个 class,它具有 Select、From 等方法,这将有助于更清晰的查询构造。在使用代码中,我只创建一个继承自 class 的匿名类型并使用这些方法。
你想要的是不可能的(至少不是你描述的那样)。
据我所知;您应该尝试模仿为 Linq 所做的事情;一个接口(如 IEnumerable),带有你想要的所有方法(或者可能是一个抽象的 class bu,阻止你从其他东西继承)+ 其他东西(如果你想要它们作为扩展方法,可能是一个模块)定义 Select等作用于界面
从那时起,您可以创建 class 实现接口并在其上使用您的自定义 Select 等