使用新文件名继承 StreamWriter

Inheriting StreamWriter with new filename

我想编写如下代码:

Public Class LogFile
    Inherits StreamWriter

    Private LogsDirectory As String = Application.StartupPath & "\" & "logs\"

    Public Sub New(shortName As String)            
        Dim fullFilePath As String = LogsDirectory & shortName & "_" & Format(Now, "HHmmss") & ".log"    
        MyBase.New(fullFilePath)
    End Sub

End Class

但是我不得不这样做(因为 Sub New 的第一条语句必须是基本构造函数):

 Public Class LogFile  
     Inherits StreamWriter       

    Public Sub New(shortName As String)              
        MyBase.New(Application.StartupPath & "\" & "logs\" & shortName & "_" & Format(Now, "HHmmss") & ".log")
    End Sub

End Class

有没有办法'around'这个?在这种情况下没关系,但可以想象,我想做更多的处理,而不适合单行。

只要函数是 Shared 就可以实现你想要的(LogsDirectory 也必须是 Shared

编译如下:

Public Class LogFile
    Inherits StreamWriter

    Public Sub New(shortName As String)
        MyBase.New(InitFunction(shortName))
    End Sub

    Private Shared LogsDirectory As String = Application.StartupPath & "\" & "logs\"

    Private Shared Function InitFunction(shortname As String) As String
        Dim fullFilePath = LogsDirectory & shortname & "_" & Format(Now, "HHmmss") & ".log"
        Return fullFilePath
    End Function
End Class