2

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

4

2 回答 2

1

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

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 选项对于使破解工作至关重要。输出:

Option Strict Off
Option Explicit On

Namespace SomeNamespace
    Public Class SomeClass
Partial _
        Private Sub SomeMethod()
        End Sub
    End Class
End Namespace
于 2009-11-30T17:27:59.707 回答
0

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

Dim onDeleting = "Partial Private Sub OnDeleting()" & Environment.NewLine & "End Sub"
type.Members.Add(New CodeSnippetTypeMember(onDeleting))
于 2009-12-02T23:13:59.543 回答