【问题标题】:Custom ToString Extension not working - VB.NET自定义 ToString 扩展不起作用 - VB.NET
【发布时间】:2012-04-18 10:38:02
【问题描述】:

我有以下代码

Module StringExtensions

<Extension()>
Public Function ToString(ByVal e As Elements) As String
    Return e(0).ToString()
End Function

<Extension()>
Public Function ToString(ByVal e As List(Of Element)) As String
    e.ToString("|")
End Function

<Extension()>
Public Function ToString(ByVal e As List(Of Element), ByVal delim As String = "|") As String
    Dim s As String = ""
    For Each e1 As Element In e
        s = String.Join(delim, s, IIf(e1.Terminal, "!", "") & e1.Name)
    Next e1
    Return s.TrimStart(delim).TrimEnd(delim)
End Function

End Module

Class Elements
Inherits List(Of Element)
'...Various properties and methods
End Class
Class Element
'...Various Properties and methods
End Class

我的问题:

由于某种原因,只能调用带有 delim 变量的 ToString 扩展。 VS2010 甚至不会显示还有其他的。 我试过只有两个 ToString 之一没有变量输入

这是为什么?

我正在使用具有不同类的类似设置,并且效果很好。 据我所知,我在语法方面没有做错任何事情。

【问题讨论】:

  • 取一个名字,任何名字,但不是 ToString。

标签: .net extension-methods tostring


【解决方案1】:

.NET 中的每个类都从根基类 object 继承 ToString()。与成员方法具有完全相同签名的扩展方法不会显示,也不能使用扩展方法语法调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2016-09-18
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多