• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

VB.NET CodeMemberField.InitExpression属性代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Sample

' 导入命名空间
Imports System.Reflection
Imports System.IO
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.CSharp


'/ <summary>
'/ This code example creates a graph using a CodeCompileUnit and  
'/ generates source code for the graph using the CSharpCodeProvider.
'/ </summary>

Class Sample
    '/ <summary>
    '/ Define the compile unit to use for code generation. 
    '/ </summary>
    Private targetUnit As CodeCompileUnit
    
    '/ <summary>
    '/ The only class in the compile unit. 
    '/ </summary>
    Private targetClass As CodeTypeDeclaration
    
    '/ <summary>
    '/ The name of the file to contain the source code.
    '/ </summary>
    Private Const outputFileName As String = "SampleCode.vb"
    
    
    '/ <summary>
    '/ Define the class.
    '/ </summary>
    Public Sub New() 
        targetUnit = New CodeCompileUnit()
        Dim samples As New CodeNamespace("CodeDOMSample")
        samples.Imports.Add(New CodeNamespaceImport("System"))
        targetClass = New CodeTypeDeclaration("CodeDOMCreatedClass")
        targetClass.IsClass = True
        targetClass.TypeAttributes = TypeAttributes.Public Or TypeAttributes.Sealed
        samples.Types.Add(targetClass)
        targetUnit.Namespaces.Add(samples)
    
    End Sub
    
    
    '/ <summary>
    '/ Adds a field to the class.
    '/ </summary>
    Public Sub AddField()

        Dim testField As New CodeMemberField()
        testField.Name = "today"
        testField.Type = New CodeTypeReference(GetType(System.DateTime))
        testField.Attributes = MemberAttributes.Private Or MemberAttributes.Static
        testField.InitExpression = New CodeFieldReferenceExpression(New CodeTypeReferenceExpression("System.DateTime"), "Today")

        targetClass.Members.Add(testField)

    End Sub
     
    
    '/ <summary>
    '/ Add a constructor to the class.
    '/ </summary>
    Public Sub AddConstructor() 
        ' Declare the constructor
        Dim constructor As New CodeConstructor()
        constructor.Attributes = MemberAttributes.Public Or MemberAttributes.Final
        
        targetClass.Members.Add(constructor)
    
    End Sub
    
    
    '/ <summary>
    '/ Add an entry point to the class.
    '/ </summary>
    Public Sub AddEntryPoint() 
        Dim start As New CodeEntryPointMethod()
        Dim objectCreate As New CodeObjectCreateExpression(New CodeTypeReference("CodeDOMCreatedClass"))
        ' Add the statement:
        ' "CodeDOMCreatedClass testClass = 
        '     new CodeDOMCreatedClass();"
        start.Statements.Add(New CodeVariableDeclarationStatement(New CodeTypeReference("CodeDOMCreatedClass"), "testClass", objectCreate))
        
        ' Creat the expression:
        ' "testClass.ToString()"
        Dim toStringInvoke As New CodeMethodInvokeExpression(New CodeVariableReferenceExpression("today"), "ToString")
        
        ' Add a System.Console.WriteLine statement with the previous 
        ' expression as a parameter.
        start.Statements.Add(New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("System.Console"), "WriteLine", toStringInvoke))
        targetClass.Members.Add(start)
    
    End Sub
    
    '/ <summary>
    '/ Generate CSharp source code from the compile unit.
    '/ </summary>
    '/ <param name="filename">Output file name</param>
    Public Sub GenerateCSharpCode(ByVal fileName As String) 
        Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VB")
        Dim options As New CodeGeneratorOptions()
        Dim sourceWriter As New StreamWriter(fileName)
        Try
            provider.GenerateCodeFromCompileUnit(targetUnit, sourceWriter, options)
        Finally
            sourceWriter.Dispose()
        End Try
    
    End Sub
    
    
    '/ <summary>
    '/ Create the CodeDOM graph and generate the code.
    '/ </summary>
    Shared Sub Main() 
        Dim sample As New Sample()
        sample.AddField()
        sample.AddConstructor()
        sample.AddEntryPoint()
        sample.GenerateCSharpCode(outputFileName)
    
    End Sub
End Class
' A Visual Basic code generator produces the following source code for the preceeding example code:
'Option Strict Off
'Option Explicit On

''Namespace CodeDOMSample

'    Public NotInheritable Class CodeDOMCreatedClass

'        Private Shared today As Date = Date.Today

'        Public Sub New()
'            MyBase.New()
'        End Sub

'        Public Shared Sub Main()
'            Dim testClass As CodeDOMCreatedClass = New CodeDOMCreatedClass
'            System.Console.WriteLine(today.ToString)
'        End Sub
'    End Class
'End Namespace
开发者ID:VB.NET开发者,项目名称:System.CodeDom,代码行数:146,代码来源:CodeMemberField.InitExpression



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET Comparer<T>.Default属性代码示例发布时间:2022-05-26
下一篇:
VB.NET AddInToken.AddInFullName属性代码示例发布时间:2022-05-26
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap