使用 CodeDom 的 VB.NET 部分方法的解决方法?

发布于 2024-08-12 17:00:43 字数 216 浏览 7 评论 0原文

我知道 CodeDom 不支持部分方法,但是有解决方法吗?我找到了 C# 的解决方法,但我需要一个用于 VB.NET 的。谢谢。

I know CodeDom doesn't support partial methods, but is there a workaround? I found a workaround for C#, but I need one for VB.NET. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2024-08-19 17:00:43

这是一种可怕的 hack,与 C# 的 hack 一样邪恶,但它确实有效:

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.IO

Module Module1

    Sub Main()
        Dim unit As New CodeCompileUnit
        Dim nspace As New CodeNamespace("SomeNamespace")
        Dim vbclass As New CodeTypeDeclaration("SomeClass")
        vbclass.IsClass = True
        Dim snippet As New CodeSnippetTypeMember("Partial _")
        vbclass.Members.Add(snippet)
        Dim method As New CodeMemberMethod()
        method.Name = "SomeMethod"
        method.Attributes = MemberAttributes.Private
        vbclass.Members.Add(method)
        nspace.Types.Add(vbclass)
        unit.Namespaces.Add(nspace)

        dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VB")
        Dim options As New CodeGeneratorOptions()
        options.BlankLinesBetweenMembers = False
        dim writer As new StringWriter()
        provider.GenerateCodeFromCompileUnit(unit, writer, options)
        Console.WriteLine(writer.ToString())
        Console.ReadLine()
    End Sub

End Module

请注意,BlankLinesBetweenMembers 选项对于使 hack 起作用至关重要。输出:

Option Strict Off
Option Explicit On

Namespace SomeNamespace
    Public Class SomeClass
Partial _
        Private Sub SomeMethod()
        End Sub
    End Class
End Namespace

It is a horrible hack, as wicked as the C# one, but it does work:

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.IO

Module Module1

    Sub Main()
        Dim unit As New CodeCompileUnit
        Dim nspace As New CodeNamespace("SomeNamespace")
        Dim vbclass As New CodeTypeDeclaration("SomeClass")
        vbclass.IsClass = True
        Dim snippet As New CodeSnippetTypeMember("Partial _")
        vbclass.Members.Add(snippet)
        Dim method As New CodeMemberMethod()
        method.Name = "SomeMethod"
        method.Attributes = MemberAttributes.Private
        vbclass.Members.Add(method)
        nspace.Types.Add(vbclass)
        unit.Namespaces.Add(nspace)

        dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VB")
        Dim options As New CodeGeneratorOptions()
        options.BlankLinesBetweenMembers = False
        dim writer As new StringWriter()
        provider.GenerateCodeFromCompileUnit(unit, writer, options)
        Console.WriteLine(writer.ToString())
        Console.ReadLine()
    End Sub

End Module

Note that the BlankLinesBetweenMembers option is crucial to make the hack work. Output:

Option Strict Off
Option Explicit On

Namespace SomeNamespace
    Public Class SomeClass
Partial _
        Private Sub SomeMethod()
        End Sub
    End Class
End Namespace
温柔戏命师 2024-08-19 17:00:43

我最终检测了语言并使用 CodeSnippetTypeMember()

Dim onDeleting = "Partial Private Sub OnDeleting()" & Environment.NewLine & "End Sub"
type.Members.Add(New CodeSnippetTypeMember(onDeleting))

I ended up detecting the language and using CodeSnippetTypeMember()

Dim onDeleting = "Partial Private Sub OnDeleting()" & Environment.NewLine & "End Sub"
type.Members.Add(New CodeSnippetTypeMember(onDeleting))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文