【发布时间】:2018-05-08 10:57:03
【问题描述】:
所以,我正在使用一个库(Coderr,任何熟悉它的人),它是用 C# 编写的,他们的示例也是如此。我的项目当然是在 VB 中,我对实现他们的示例类的模板有点困惑。
他们现有的一个类的一个例子是here。
我尝试手动移植它,但失败了,所以使用 Telerik 的 C# 到 VB 转换器。它输出的代码看起来很不错,而且只需进行一次调整,该类本身就可以很好地工作。除了继承子句。
我现在得到的代码:
Namespace codeRR.Client.AspNet.CurrentUserContext
Public Class CurrentUserContext
Inherits IContextInfoProvider
Public Function Collect(ByVal context As IErrorReporterContext) As ContextCollectionDTO
Dim CurrentUser As CurrentUser = CType(HttpContext.Current.Session("CurrentUser"), CurrentUser)
If CurrentUser Is Nothing Then Return Nothing
Dim converter = New ObjectToContextCollectionConverter()
Dim collection = converter.Convert(Name, CurrentUser)
Return If(collection.Properties.Count = 0, Nothing, collection)
End Function
Public ReadOnly Property Name As String
Get
Return "CurrentUser"
End Get
End Property
End Class
End Namespace
与示例代码非常相似,但基本上是返回我的自定义对象。 Inherits 行失败:
类只能从其他类继承。
这是有道理的,因为IContextInfoProvider 是一个接口。
我只是坚持我如何在 VB 中实际使用它。我需要将该类插入一个接受IContextInfoProvider 类型对象的函数中。
【问题讨论】:
-
您可以在 VB 项目中引用 c# 库。为什么不直接将 Coderr 编译成 dll 并作为参考添加呢?
-
在 VB.NET 中,您使用
Inherits从基类继承,并使用Implements实现接口。 -
这是带有 C# lisp 的 vb.net 代码 :) 在 vb.net 中,您不能继承接口,只能实现一个接口。顺便说一句,语法很合理。