Imports System Imports System.IO Imports System.CodeDom Module Module1 Sub Main(ByVal args As String()) 'メソッド宣言 Dim method As New CodeMemberMethod() method.Name = "MyAdd" method.Parameters.Add(New CodeParameterDeclarationExpression(GetType(Integer), "iA")) method.Parameters.Add(New CodeParameterDeclarationExpression(GetType(Integer), "iB")) method.ReturnType = New CodeTypeReference(GetType(Integer)) '加算処理 Dim arg1 As New CodeArgumentReferenceExpression("iA") Dim arg2 As New CodeArgumentReferenceExpression("iB") Dim expression As New CodeBinaryOperatorExpression( _ arg1, _ CodeBinaryOperatorType.Add, _ arg2) '戻り値として返す Dim returnStatement As New CodeMethodReturnStatement(expression) method.Statements.Add(returnStatement) 'ソース化 Dim generatedSource As String Using sw As New StringWriter() Dim provider As New Microsoft.VisualBasic.VBCodeProvider() 'VB用 'Dim provider As New Microsoft.CSharp.CSharpCodeProvider() 'C#用 'Dim provider As New Microsoft.VisualC.CppCodeProvider() 'VC用 'Dim provider As New Microsoft.JScript.JScriptCodeProvider() 'JScript用 'Dim provider As New Microsoft.Msil.MsilCodeProvider() 'MSIL用 Try provider.GenerateCodeFromMember(method, sw, Nothing) Catch ex As NotImplementedException For Each s As CodeStatement In method.Statements provider.GenerateCodeFromStatement(s, sw, Nothing) Next End Try sw.Flush() generatedSource = sw.ToString() End Using MsgBox(generatedSource) End Sub End Module