我想将自己的成员添加到StringBuilder类中,但是当我去创建它时,IntelliSense不会提起它。

public class myStringBuilder()
    Inherits System.Text.[StringBuilder should be here]
    ....
end class

甚至可能吗?感谢

有帮助吗?

解决方案

StringBuilder NotInheritable (又名C#中的密封),因此您无法从中获取。您可以尝试在自己的类中包装 StringBuilder ,或者考虑使用扩展方法

其他提示

不, StringBuilder NotInheritable 类。您可以尝试包装 StringBuilder 实例,但是可以不继承它。如果您使用的是.NET,也可以使用扩展方法 3.5。

这是我想出来的,对于那些好奇的人:

Imports System.Runtime.CompilerServices
Module sbExtension
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, arg0), ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, ByVal arg0 As Object, _
                                   ByVal arg1 As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, arg0, arg1), ControlChars.NewLine)
    End Sub
    <Extension()> _
    Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                   ByVal format As String, _
                                   ByVal arg0 As Object, _
                                   ByVal arg1 As Object, _
                                   ByVal arg2 As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, arg0, arg1, arg2), ControlChars.NewLine)
    End Sub
    <Extension()> _
   Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _
                                  ByVal format As String, _
                                  ByVal ParamArray args() As Object)
        oStr.AppendFormat("{0}{1}", String.Format(format, args), ControlChars.NewLine)
    End Sub
End Module

StringBuilder是一个密封的类......所以不允许继承。

StringBuilder是密封的。你无法继承它。

如果您使用的是早期版本的.Net,您可以编写基本相同的StringBuilderExtensions类,然后显式调用静态方法。

使用.Net 3.5: myStringBuilder.MyExtensionMethod(etc ...);

Pre .Net 3.5: StringBuilderExtensions.MyExtensionMethod(myStringBuilder等);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top