当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET CodeDomProvider.GenerateCodeFromMember方法代码示例

本文整理汇总了VB.NET中System.CodeDom.Compiler.CodeDomProvider.GenerateCodeFromMember方法的典型用法代码示例。如果您正苦于以下问题:VB.NET CodeDomProvider.GenerateCodeFromMember方法的具体用法?VB.NET CodeDomProvider.GenerateCodeFromMember怎么用?VB.NET CodeDomProvider.GenerateCodeFromMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.CodeDom.Compiler.CodeDomProvider的用法示例。


在下文中一共展示了CodeDomProvider.GenerateCodeFromMember方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Program

' 导入命名空间
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Text.RegularExpressions



Class Program
    Private Shared providerName As String = "vb"
    Private Shared sourceFileName As String = "test.vb"
    Private Shared snippetMethod As CodeSnippetTypeMember
    
    Shared Sub Main(ByVal args() As String) 
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider(providerName)
        
        ' Create a code snippet to be used in the graph.
        GenCodeFromMember(provider, New CodeGeneratorOptions())
        
        LogMessage("Building CodeDOM graph...")
        
        Dim cu As New CodeCompileUnit()
        
        cu = BuildClass1()
        
        Dim sw As New StringWriter()
        
        LogMessage("Generating code...")
        provider.GenerateCodeFromCompileUnit(cu, sw, Nothing)
        
        Dim output As String = sw.ToString()
        
        LogMessage("Dumping source...")
        LogMessage(output)
        
        LogMessage("Writing source to file...")
        Dim s As Stream = File.Open(sourceFileName, FileMode.Create)
        Dim t As New StreamWriter(s)
        t.Write(output)
        t.Close()
        s.Close()
        
        Dim opt As New CompilerParameters(New String() {"System.dll"})
        opt.GenerateExecutable = False
        opt.OutputAssembly = "Sample.dll"
        
        Dim results As CompilerResults
        
        LogMessage(("Compiling with " + providerName))
        results = provider.CompileAssemblyFromFile(opt, sourceFileName)
        
        OutputResults(results)
        If results.NativeCompilerReturnValue <> 0 Then
            LogMessage("")
            LogMessage("Compilation failed.")
        Else
            LogMessage("")
            LogMessage("Demo completed successfully.")
        End If
        File.Delete(sourceFileName)
    
    End Sub
    
    
    ' Build a library program graph using 
    ' System.CodeDom types.
    Public Shared Function BuildClass1() As CodeCompileUnit 
        ' Create a new CodeCompileUnit to contain 
        ' the program graph.
        Dim compileUnit As New CodeCompileUnit()
        
        ' Declare a new namespace called Samples.
        Dim samples As New CodeNamespace("Samples")
        ' Add the new namespace to the compile unit.
        compileUnit.Namespaces.Add(samples)
        
        ' Add the new namespace import for the System namespace.
        samples.Imports.Add(New CodeNamespaceImport("System"))
        
        ' Declare a new type called Class1.
        Dim class1 As New CodeTypeDeclaration("Class1")
        
        ' Add the new type to the namespace type collection.
        samples.Types.Add(class1)
        
        class1.Members.Add(snippetMethod)
        
        Return compileUnit
    
    End Function 'BuildClass1
    
    Shared Sub LogMessage(ByVal [text] As String) 
        Console.WriteLine([text])
    
    End Sub
    
    
    Shared Sub OutputResults(ByVal results As CompilerResults) 
        LogMessage(("NativeCompilerReturnValue=" + results.NativeCompilerReturnValue.ToString()))
        Dim s As String
        For Each s In  results.Output
            LogMessage(s)
        Next s
    
    End Sub
    
    Shared Sub GenCodeFromMember(ByVal provider As CodeDomProvider, ByVal options As CodeGeneratorOptions) 
        options.BracingStyle = "C"
        Dim method1 As New CodeMemberMethod()
        method1.Name = "ReturnString"
        method1.Attributes = MemberAttributes.Public
        method1.ReturnType = New CodeTypeReference("System.String")
        method1.Parameters.Add(New CodeParameterDeclarationExpression("System.String", "text"))
        method1.Statements.Add(New CodeMethodReturnStatement(New CodeArgumentReferenceExpression("text")))
        Dim sw As New StringWriter()
        provider.GenerateCodeFromMember(method1, sw, options)
        snippetMethod = New CodeSnippetTypeMember(sw.ToString())
    
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.CodeDom.Compiler,代码行数:120,代码来源:CodeDomProvider.GenerateCodeFromMember


注:本文中的System.CodeDom.Compiler.CodeDomProvider.GenerateCodeFromMember方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。