mygenerator.vb

来自「大名鼎鼎的mono是.NET平台的跨平台(支持linux」· VB 代码 · 共 633 行 · 第 1/3 页

VB
633
字号
' ' Visual Basic.Net Compiler' Copyright (C) 2004 - 2007 Rolf Bjarne Kvinge, RKvinge@novell.com' ' This library is free software; you can redistribute it and/or' modify it under the terms of the GNU Lesser General Public' License as published by the Free Software Foundation; either' version 2.1 of the License, or (at your option) any later version.' ' This library is distributed in the hope that it will be useful,' but WITHOUT ANY WARRANTY; without even the implied warranty of' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU' Lesser General Public License for more details.' ' You should have received a copy of the GNU Lesser General Public' License along with this library; if not, write to the Free Software' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA' Public Class MyGenerator    Private m_Compiler As Compiler    Private m_Code As System.Text.StringBuilder    Private m_ProjectCode As System.Text.StringBuilder    Private m_MyType As MyTypes    Private m_GlobalMy As String    Sub New(ByVal Compiler As Compiler)        m_Compiler = Compiler    End Sub    ReadOnly Property Compiler() As Compiler        Get            Return m_Compiler        End Get    End Property    ReadOnly Property Code() As System.Text.StringBuilder        Get            If m_Code Is Nothing Then m_Code = New System.Text.StringBuilder()            Return m_Code        End Get    End Property    ReadOnly Property ProjectCode() As System.Text.StringBuilder        Get            If m_ProjectCode Is Nothing Then m_ProjectCode = New System.Text.StringBuilder            Return m_ProjectCode        End Get    End Property    Function Generate() As Boolean        Dim result As Boolean = True        Dim _MyTypeDefine As Define        Dim _MyType As String        If Compiler.CommandLine.NoVBRuntimeRef Then Return result        _MyTypeDefine = Compiler.CommandLine.Define("_MYTYPE")        If _MyTypeDefine Is Nothing Then            _MyType = String.Empty        Else            _MyType = _MyTypeDefine.ValueAsString        End If        If _MyType = String.Empty Then            m_MyType = MyTypes.Windows        ElseIf [Enum].IsDefined(GetType(MyTypes), _MyType) Then            m_MyType = CType([Enum].Parse(GetType(MyTypes), _MyType, False), MyTypes)        Else            m_MyType = MyTypes.Empty        End If        If m_MyType = MyTypes.Empty Then            Return True        End If        If Compiler.CommandLine.RootNamespace <> "" Then            m_GlobalMy = "Global." & Compiler.CommandLine.RootNamespace & ".My"        Else            m_GlobalMy = "Global" & ".My"        End If        result = GenerateMyApplication() AndAlso result        result = GenerateMyComputer() AndAlso result        result = GenerateMyForms() AndAlso result        result = GenerateMyLog() AndAlso result        result = GenerateMyRequest() AndAlso result        result = GenerateMyResources() AndAlso result        result = GenerateMyResponse() AndAlso result        result = GenerateMySettings() AndAlso result        result = GenerateMyUser() AndAlso result        result = GenerateMyWebServices() AndAlso result        If Code.Length > 0 Then            Dim projectPrepend As New System.Text.StringBuilder()            projectPrepend.AppendLine("    <Global.System.CodeDom.Compiler.GeneratedCode(""MyTemplate"", ""8.0.0.0"")> _")            projectPrepend.AppendLine("    <Global.Microsoft.VisualBasic.HideModuleName> _")            projectPrepend.AppendLine("    Friend Module MyProject")            projectPrepend.AppendLine("        <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _")            projectPrepend.AppendLine("        <Global.System.Runtime.InteropServices.ComVisible(False)> _")            projectPrepend.AppendLine("        Friend NotInheritable Class ThreadSafeObjectProvider(Of T As New)")            projectPrepend.AppendLine("            ")            projectPrepend.AppendLine("            <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _")            projectPrepend.AppendLine("            <Global.System.Diagnostics.DebuggerHidden> _")            projectPrepend.AppendLine("            Public Sub New()")            projectPrepend.AppendLine("            End Sub")            projectPrepend.AppendLine("            ")            If Compiler.CommandLine.Target = CommandLine.Targets.Library Then                projectPrepend.AppendLine("            Private m_Context As New Global.Microsoft.VisualBasic.MyServices.Internal.ContextValue(Of T)")                projectPrepend.AppendLine("            Friend ReadOnly Property GetInstance As T")                projectPrepend.AppendLine("                Get")                projectPrepend.AppendLine("                    Dim tmp as T = m_Context.Value")                projectPrepend.AppendLine("                    If tmp Is Nothing Then")                projectPrepend.AppendLine("                        tmp = Global.System.Activator.CreateInstance(Of T)")                projectPrepend.AppendLine("                        m_Context.Value = tmp")                projectPrepend.AppendLine("                    End If")                projectPrepend.AppendLine("                    Return tmp")                projectPrepend.AppendLine("                End Get")                projectPrepend.AppendLine("            End Property")            Else                projectPrepend.AppendLine("            <Global.System.Runtime.CompilerServices.CompilerGenerated> _")                projectPrepend.AppendLine("            <Global.System.ThreadStatic> _")                projectPrepend.AppendLine("            Private Shared m_ThreadStaticValue As T")                projectPrepend.AppendLine("            ")                projectPrepend.AppendLine("            Friend ReadOnly Property GetInstance As T")                projectPrepend.AppendLine("                Get")                projectPrepend.AppendLine("                    If (m_ThreadStaticValue Is Nothing) Then")                projectPrepend.AppendLine("                        m_ThreadStaticValue = Global.System.Activator.CreateInstance(Of T)")                projectPrepend.AppendLine("                    End If")                projectPrepend.AppendLine("                    Return m_ThreadStaticValue")                projectPrepend.AppendLine("                End Get")                projectPrepend.AppendLine("            End Property")            End If            projectPrepend.AppendLine("        End Class")            ProjectCode.Insert(0, projectPrepend)            ProjectCode.AppendLine("    End Module")            Code.Insert(0, "Namespace My" & Environment.NewLine)            Code.Append(ProjectCode)            Code.AppendLine("End Namespace")            Code.Replace("$GLOBALMY$", m_GlobalMy)#If DEBUG AndAlso False Then            Dim counter As Integer = 1            For Each line As String In VB.Split(Code.ToString, VB.vbNewLine)                Compiler.Report.WriteLine(counter & ": " & line)                counter += 1            Next#End If            Compiler.CommandLine.Files.Add(New CodeFile("<MyGenerator>", "<MyGenerator>", Compiler, Code.ToString))        End If        Return result    End Function    Function GenerateMyApplication() As Boolean        Dim result As Boolean = True        Dim _MyApplicationDefine As Define        Dim _MyApplication As String        _MyApplicationDefine = Compiler.CommandLine.Define("_MYAPPLICATIONTYPE")        If _MyApplicationDefine Is Nothing Then            Select Case m_MyType                Case MyTypes.Console, MyTypes.WindowsFormsWithCustomSubMain                    _MyApplication = "Console"                Case MyTypes.Windows                    _MyApplication = "Windows"                Case MyTypes.WindowsForms                    _MyApplication = "WindowsForms"                Case Else                    _MyApplication = String.Empty            End Select        Else            _MyApplication = _MyApplicationDefine.Value        End If        Dim baseClass As String        Select Case _MyApplication            Case "Console"                baseClass = "Global.Microsoft.VisualBasic.ApplicationServices.ConsoleApplicationBase"            Case "Windows"                baseClass = "Global.Microsoft.VisualBasic.ApplicationServices.ApplicationBase"            Case "WindowsForms"                baseClass = "Global.Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase"            Case Else                Return True        End Select        Code.AppendLine("    <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Never)> _")        Code.AppendLine("    <Global.System.CodeDom.Compiler.GeneratedCode(""MyTemplate"", ""8.0.0.0"")> _")        Code.AppendLine("    Friend Class MyApplication")        Code.Append("        Inherits ") : Code.AppendLine(baseClass)        'Code.AppendLine("        Public Sub New()")        'Code.AppendLine("        End Sub")        If Compiler.CommandLine.Target = CommandLine.Targets.Winexe AndAlso Compiler.CommandLine.Main = String.Empty AndAlso baseClass = "Global.Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase" Then            Code.AppendLine("        <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)> _")            Code.AppendLine("        <Global.System.Diagnostics.DebuggerHidden()> _")            Code.AppendLine("        Friend Shared Sub Main(ByVal Args As String())")            Code.AppendLine("            Global.System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(Global.Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.UseCompatibleTextRendering)")            Code.AppendLine("            $GLOBALMY$.MyProject.Application.Run(Args)")            Code.AppendLine("        End Sub")        End If        Code.AppendLine("    End Class")        ProjectCode.AppendLine("        Private Shared ReadOnly m_AppObjectProvider As ThreadSafeObjectProvider(Of $GLOBALMY$.MyApplication) = New ThreadSafeObjectProvider(Of $GLOBALMY$.MyApplication)")        ProjectCode.AppendLine("        <Global.System.ComponentModel.Design.HelpKeyword(""My.Application"")> _")        ProjectCode.AppendLine("        Friend Shared ReadOnly Property Application As $GLOBALMY$.MyApplication")        ProjectCode.AppendLine("            <Global.System.Diagnostics.DebuggerHidden()> _")        ProjectCode.AppendLine("            Get")        ProjectCode.AppendLine("                Return m_AppObjectProvider.GetInstance")        ProjectCode.AppendLine("            End Get")

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?