1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
Imports System
Imports System.Reflection
Imports System.IO
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic
' This code example creates a graph using a CodeCompileUnit and
' generates source code for the graph using the VBCodeProvider.
Class Sample
' Define the compile unit to use for code generation.
Private targetUnit As CodeCompileUnit
' The only class in the compile unit. This class contains 2 fields,
' 3 properties, a constructor, an entry point, and 1 simple method.
Private targetClass As CodeTypeDeclaration
' The name of the file to contain the source code.
Private Const outputFileName As String = "SampleCode.vb"
' Define the class.
Public Sub New()
targetUnit = New CodeCompileUnit()
Dim samples As New CodeNamespace("CodeDOMSample")
samples.Imports.Add(New CodeNamespaceImport("System"))
'voici le nom du type !!!!
targetClass = New CodeTypeDeclaration("CodeDOMCreatedClass")
targetClass.IsClass = True
targetClass.TypeAttributes = _
TypeAttributes.Public Or TypeAttributes.Sealed
samples.Types.Add(targetClass)
targetUnit.Namespaces.Add(samples)
End Sub 'New
' Adds two fields to the class.
Public Sub AddFields()
' Declare the widthValue field.
Dim widthValueField As New CodeMemberField()
widthValueField.Attributes = MemberAttributes.Private
widthValueField.Name = "widthValue"
widthValueField.Type = _
New CodeTypeReference(GetType(System.Double))
widthValueField.Comments.Add(New CodeCommentStatement( _
"The width of the object."))
targetClass.Members.Add(widthValueField)
' Declare the heightValue field
Dim heightValueField As New CodeMemberField()
heightValueField.Attributes = MemberAttributes.Private
heightValueField.Name = "heightValue"
heightValueField.Type = _
New CodeTypeReference(GetType(System.Double))
heightValueField.Comments.Add(New CodeCommentStatement( _
"The height of the object."))
targetClass.Members.Add(heightValueField)
End Sub 'AddFields
' Add three properties to the class.
Public Sub AddProperties()
' Declare the read only Width property.
Dim widthProperty As New CodeMemberProperty()
widthProperty.Attributes = _
MemberAttributes.Public Or MemberAttributes.Final
widthProperty.Name = "Width"
widthProperty.HasGet = True
widthProperty.Type = New CodeTypeReference(GetType(System.Double))
widthProperty.Comments.Add(New CodeCommentStatement( _
"The width property for the object."))
widthProperty.GetStatements.Add(New CodeMethodReturnStatement( _
New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "widthValue")))
targetClass.Members.Add(widthProperty)
' Declare the read-only Height property.
Dim heightProperty As New CodeMemberProperty()
heightProperty.Attributes = _
MemberAttributes.Public Or MemberAttributes.Final
heightProperty.Name = "Height"
heightProperty.HasGet = True
heightProperty.Type = New CodeTypeReference(GetType(System.Double))
heightProperty.Comments.Add(New CodeCommentStatement( _
"The Height property for the object."))
heightProperty.GetStatements.Add(New CodeMethodReturnStatement( _
New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "heightValue")))
targetClass.Members.Add(heightProperty)
' Declare the read only Area property.
Dim areaProperty As New CodeMemberProperty()
areaProperty.Attributes = _
MemberAttributes.Public Or MemberAttributes.Final
areaProperty.Name = "Area"
areaProperty.HasGet = True
areaProperty.Type = New CodeTypeReference(GetType(System.Double))
areaProperty.Comments.Add(New CodeCommentStatement( _
"The Area property for the object."))
' Create an expression to calculate the area for the get accessor
' of the Area property.
Dim areaExpression As New CodeBinaryOperatorExpression( _
New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "widthValue"), _
CodeBinaryOperatorType.Multiply, _
New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "heightValue"))
areaProperty.GetStatements.Add( _
New CodeMethodReturnStatement(areaExpression))
targetClass.Members.Add(areaProperty)
End Sub 'AddProperties
' Adds a method to the class. This method multiplies values stored
' in both fields.
Public Sub AddMethod()
' Declaring a ToString method.
Dim toStringMethod As New CodeMemberMethod()
toStringMethod.Attributes = _
MemberAttributes.Public Or MemberAttributes.Override
toStringMethod.Name = "ToString"
toStringMethod.ReturnType = _
New CodeTypeReference(GetType(System.String))
Dim widthReference As New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "Width")
Dim heightReference As New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "Height")
Dim areaReference As New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "Area")
' Declaring a return statement for method ToString.
Dim returnStatement As New CodeMethodReturnStatement()
' This statement returns a string representation of the width,
' height, and area.
Dim formattedOutput As String = "The object:" & Environment.NewLine _
& " width = {0}," & Environment.NewLine & " height = {1}," _
& Environment.NewLine & " area = {2}"
returnStatement.Expression = New CodeMethodInvokeExpression( _
New CodeTypeReferenceExpression("System.String"), "Format", _
New CodePrimitiveExpression(formattedOutput), widthReference, _
heightReference, areaReference)
toStringMethod.Statements.Add(returnStatement)
targetClass.Members.Add(toStringMethod)
End Sub 'AddMethod
' Add a constructor to the class.
Public Sub AddConstructor()
' Declare the constructor
Dim constructor As New CodeConstructor()
constructor.Attributes = _
MemberAttributes.Public Or MemberAttributes.Final
' Add parameters.
constructor.Parameters.Add( _
New CodeParameterDeclarationExpression( _
GetType(System.Double), "width"))
constructor.Parameters.Add( _
New CodeParameterDeclarationExpression( _
GetType(System.Double), "height"))
' Add field initialization logic
Dim widthReference As New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "widthValue")
constructor.Statements.Add(New CodeAssignStatement( _
widthReference, New CodeArgumentReferenceExpression("width")))
Dim heightReference As New CodeFieldReferenceExpression( _
New CodeThisReferenceExpression(), "heightValue")
constructor.Statements.Add( _
New CodeAssignStatement(heightReference, _
New CodeArgumentReferenceExpression("height")))
targetClass.Members.Add(constructor)
End Sub 'AddConstructor
' Add an entry point to the class.
Public Sub AddEntryPoint()
Dim start As New CodeEntryPointMethod()
Dim objectCreate As New CodeObjectCreateExpression( _
New CodeTypeReference("CodeDOMCreatedClass"), _
New CodePrimitiveExpression(5.3), _
New CodePrimitiveExpression(6.9))
' Add the statement:
' "CodeDOMCreatedClass testClass = _
' new CodeDOMCreatedClass(5.3, 6.9);"
start.Statements.Add(New CodeVariableDeclarationStatement( _
New CodeTypeReference("CodeDOMCreatedClass"), _
"testClass", objectCreate))
' Creat the expression:
' "testClass.ToString()"
Dim toStringInvoke As New CodeMethodInvokeExpression( _
New CodeVariableReferenceExpression("testClass"), "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 'AddEntryPoint
' Generate Visual Basic source code from the compile unit.
Public Sub GenerateVBCode(ByVal fileName As String)
Dim provider As CodeDomProvider
provider = CodeDomProvider.CreateProvider("VisualBasic")
Dim options As New CodeGeneratorOptions()
Dim sourceWriter As New StreamWriter(fileName)
Try
provider.GenerateCodeFromCompileUnit( _
targetUnit, sourceWriter, options)
Finally
sourceWriter.Dispose()
End Try
End Sub 'GenerateVBCode
' Create the CodeDOM graph and generate the code.
Shared Sub Main()
Dim sample As New Sample()
sample.AddFields()
sample.AddProperties()
sample.AddMethod()
sample.AddConstructor()
sample.AddEntryPoint()
sample.GenerateVBCode(outputFileName)
End Sub 'Main
End Class 'Sample |
Partager